Topic: List-initialization: suspicious example


Author: Inconnu <shirarenai@yandex.ru>
Date: Mon, 10 May 2010 15:18:18 CST
Raw View
On 8 =D0=BC=D0=B0=D0=B9, 03:55, "Johannes Schaub (litb)" <schaub-johan...@w=
eb.de>
wrote:
> I think this is valid code and i agree with your analysis that it works
> fine.

But it really intended? Somewhat strange.

> ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ:http://www.comeaucomputing.com/csc/faq.html                     ]



--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: David Krauss <potswa@gmail.com>
Date: Sat, 15 May 2010 20:18:04 CST
Raw View
On May 4, 11:29 pm, Inconnu <shirare...@yandex.ru> wrote:
> Is the following example correct (i.e. compiles without error)
> according to the latest draft?
>
> #include <initializer_list>
>
> struct Z {}; // dummy class
>
> struct A
> {
>     A (std::initializer_list<int>);
>
> };
>
> struct B
> {
>     B (std::initializer_list<Z>); // adding such constr. to every
>                                           // class; it is never
> viable
>     B (A);
>
> };
>
> struct C
> {
>     C (std::initializer_list<Z>);
>     C (B);
>
> };
>
> struct B
> {
>     D (std::initializer_list<Z>);
>     D (C);
>
> };
>
> D d {1, 2, 3};

No. There is a typo at "struct B { D...," and no more than one user-
defined conversion is allowed in initialization. It doesn't compile in
GCC 4.5.

Is Z supposed to do something?


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Sun, 16 May 2010 22:22:27 CST
Raw View
David Krauss wrote:

> On May 4, 11:29 pm, Inconnu <shirare...@yandex.ru> wrote:
>> Is the following example correct (i.e. compiles without error)
>> according to the latest draft?
>>
>> #include <initializer_list>
>>
>> struct Z {}; // dummy class
>>
>> struct A
>> {
>>     A (std::initializer_list<int>);
>>
>> };
>>
>> struct B
>> {
>>     B (std::initializer_list<Z>); // adding such constr. to every
>>                                           // class; it is never
>> viable
>>     B (A);
>>
>> };
>>
>> struct C
>> {
>>     C (std::initializer_list<Z>);
>>     C (B);
>>
>> };
>>
>> struct B
>> {
>>     D (std::initializer_list<Z>);
>>     D (C);
>>
>> };
>>
>> D d {1, 2, 3};
>
> No. There is a typo at "struct B { D...," and no more than one user-
> defined conversion is allowed in initialization. It doesn't compile in
> GCC 4.5.
>
"and no more than one user-defined conversion is allowed in initialization"
=> that doesn't apply to list-initializaton though, i think. See
http://stackoverflow.com/questions/2357452/stdinitializer-list-as-function-
argument/2357688#2357688

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: David Krauss <potswa@gmail.com>
Date: Mon, 17 May 2010 03:02:06 CST
Raw View
On May 16, 11:22 pm, "Johannes Schaub (litb)" <schaub-johan...@web.de>
wrote:
> "and no more than one user-defined conversion is allowed in initialization"
> => that doesn't apply to list-initializaton though, i think. Seehttp://stackoverflow.com/questions/2357452/stdinitializer-list-as-function-argument/2357688#2357688

That increases the number of conversion constructors to two, namely
A::A(std::initializer_list<int>) and B::B(A), but OP is additionally
trying to run C::C(B), which makes three.


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Inconnu <shirarenai@yandex.ru>
Date: Tue, 18 May 2010 14:16:15 CST
Raw View
On 16 =D0=BC=D0=B0=D0=B9, 06:18, David Krauss <pot...@gmail.com> wrote:
>
>
> No. There is a typo at "struct B { D...,"
Yes, there is a typo. Should have been "struct D { D...,"

> and no more than one user-
> defined conversion is allowed in initialization. It doesn't compile in
> GCC 4.5.

That's the point. I don't see in the draft that this restriction
(about no more than one user-defined conversion) applies to the list-
initialization (and not only to implicit conversions of expressions).
Please, give the exact reference to the wording in the standard which
forbids it.

> Is Z supposed to do something?
No. Z is dummy class. Constructors with std::initializer_list<Z> are
included so that braced_initializer_list be considered a single
argument.




--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Inconnu <shirarenai@yandex.ru>
Date: Tue, 4 May 2010 22:29:06 CST
Raw View
Is the following example correct (i.e. compiles without error)
according to the latest draft?

#include <initializer_list>

struct Z {}; // dummy class

struct A
{
    A (std::initializer_list<int>);

};

struct B
{
    B (std::initializer_list<Z>); // adding such constr. to every
                                          // class; it is never
viable
    B (A);

};

struct C
{
    C (std::initializer_list<Z>);
    C (B);

};

struct B
{
    D (std::initializer_list<Z>);
    D (C);

};

D d {1, 2, 3};

The reason is that there is no difference between direct-
list-initialization and copy-list-initialization in the process of
overload resolution. Besides, if there is an initializer-list-
constructor in the class, the braced-init-list is considered a single
argument and all constructors are candidates for overload resolution.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Fri, 7 May 2010 17:55:44 CST
Raw View
Inconnu wrote:

> Is the following example correct (i.e. compiles without error)
> according to the latest draft?
>
> #include <initializer_list>
>
> struct Z {}; // dummy class
>
> struct A
> {
>     A (std::initializer_list<int>);
>
> };
>
> struct B
> {
>     B (std::initializer_list<Z>); // adding such constr. to every
>                                           // class; it is never
> viable
>     B (A);
>
> };
>
> struct C
> {
>     C (std::initializer_list<Z>);
>     C (B);
>
> };
>
> struct B
> {
>     D (std::initializer_list<Z>);
>     D (C);
>
> };
>
> D d {1, 2, 3};
>
> The reason is that there is no difference between direct-
> list-initialization and copy-list-initialization in the process of
> overload resolution. Besides, if there is an initializer-list-
> constructor in the class, the braced-init-list is considered a single
> argument and all constructors are candidates for overload resolution.
>

I think this is valid code and i agree with your analysis that it works
fine.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]