Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #68235

Re: {}

From Manfred <noname@add.invalid>
Newsgroups comp.lang.c++
Subject Re: {}
Date 2019-09-05 20:52 +0200
Organization Aioe.org NNTP Server
Message-ID <qkrlh9$bdm$1@gioia.aioe.org> (permalink)
References <7e1483e8-b52a-42a6-be3d-5e0e8ee9b437@googlegroups.com> <gtafsoFnafoU2@mid.individual.net> <qkqu3r$p9g$1@gioia.aioe.org> <4866a84c-cc09-4ba2-920f-c325a6045fb5@googlegroups.com>

Show all headers | View raw


On 9/5/2019 5:24 PM, Öö Tiib wrote:
> On Thursday, 5 September 2019 15:12:56 UTC+3, Manfred  wrote:
>> On 9/4/2019 8:59 PM, Ian Collins wrote:
>>> On 05/09/2019 00:58, Daniel wrote:
>>>> Consider
>>>>
>>>> #include <iostream>
>>>> #include <initializer_list>
>>>>
>>>> struct arg_t
>>>> {
>>>> };
>>>>
>>>> constexpr arg_t arg = arg_t();
>>>>
>>>> class C
>>>> {
>>>> public:
>>>>       C(size_t n)    {}
>>>> };
>>>>
>>>> class A
>>>> {
>>>> public:
>>>>       explicit A(size_t n)
>>>>       {
>>>>      std::cout << "1\n";
>>>>       }
>>>>       A(std::initializer_list<C> item)
>>>>       {
>>>>      std::cout << "2\n";
>>>>       }
>>>>       A(arg_t, size_t n)
>>>>       {
>>>>           std::cout << "3\n";
>>>>       }
>>>>       A(arg_t, std::initializer_list<C> item)
>>>>       {
>>>>           std::cout << "4\n";
>>>>       }
>>>> };
>>>>
>>>> int main()
>>>> {
>>>>       A a{ { 2 } };  // (1)
>>>>       A b{ arg, { 2 } };  // (2)
>>>> }
>>>>
>>>> Output (vs2019):
>>>>
>>>> 2
>>>> 3
>>>>
>>>> I don't understand why one of these constructors is using the
>>>> initializer_list, and the other is not. Explanation?
>>>
>>> Does the compiler issue any warnings?
>>>
>>> gcc gives your expected result (2,4), clang thinks you intended to call
>>> A(arg_t, size_t n) and issues a warning
>>>
>>> /tmp/x.cc:40:15: warning: braces around scalar initializer
>>> [-Wbraced-scalar-init]
>>>       A b{ arg, { 2 } };  // (2)
>>>                 ^~~~~~
>>>
>>> I'm not sure which one is correct in this case!
>>>
>>> Cheers,
>>> Ian.
>>
>> To me, it looks like gcc is the correct one.
> 
> It may be unsure and so it is bad that only clang warns about it.
> The [dcl.init.list] has felt ambiguous since 2011 and so
> gcc, clang, MSVC and ICC have had slight differences with
> those initializer_lists.
> Current issue seems to be about differences of interpretation
> of "list has a single element" special cases in it.
>   
> 

Thanks for the link.
Indeed the wording seems ambiguous, and even the examples in (1.10) are 
of limited help:
     int a = {1};
     ...
     return { "Norah" }; // return list of one element

What would be the rationale to distinguish between these two cases?

Reading on, you probably mean clause (3.8). Here the examples focus on 
the constructor syntax only, but indeed there is a problem on where a 
single object or a list of one object is initialized.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

{} Daniel <danielaparker@gmail.com> - 2019-09-04 05:58 -0700
  Re: {} Ian Collins <ian-news@hotmail.com> - 2019-09-05 06:59 +1200
    Re: {} Daniel <danielaparker@gmail.com> - 2019-09-04 12:48 -0700
    Re: {} Manfred <noname@add.invalid> - 2019-09-05 14:12 +0200
      Re: {} Öö Tiib <ootiib@hot.ee> - 2019-09-05 08:24 -0700
        Re: {} Manfred <noname@add.invalid> - 2019-09-05 20:52 +0200
        Re: {} Daniel <danielaparker@gmail.com> - 2019-09-05 13:23 -0700
          Re: {} Öö Tiib <ootiib@hot.ee> - 2019-09-05 23:44 -0700
            Re: {} Öö Tiib <ootiib@hot.ee> - 2019-09-06 02:06 -0700
              Re: {} Daniel <danielaparker@gmail.com> - 2019-09-06 11:27 -0700

csiph-web