Topic: Constructor initializer with default args


Author: "george.ryan@gmail.com" <george.ryan@gmail.com>
Date: Sun, 24 May 2009 14:57:13 CST
Raw View
I am assuming this will work in 0x?

struct C {
 C( int i, int j = 0 ) i_(i), j_(j) {}
 int i_;
 int j_;
};

C c1{ 10 }; // OK

I didn't that covered in 12.6.1, but it might be elsewhere.

--
[ 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: daniel.kruegler@googlemail.com
Date: Sun, 24 May 2009 16:07:30 CST
Raw View
On 24 Mai, 22:57, "george.r...@gmail.com" <george.r...@gmail.com>
wrote:
> I am assuming this will work in 0x?
>
> struct C {
>  C( int i, int j = 0 ) i_(i), j_(j) {}
>  int i_;
>  int j_;
>
> };
>
> C c1{ 10 }; // OK
>
> I didn't that covered in 12.6.1, but it might be elsewhere.

According to your assumption the initialization syntax used
is well-formed. It's not described in 12.6., but in 8.5 [dcl.init]:

1) p. 15 says that this is /direct-initialization/
2) We follow the conditional rule chain in p. 16 to deduce
the semantic. The first bullet which applies is b. 3:

"If the initializer is a braced-init-list, the object is list-
initialized (8.5.4)."

3) We go directly to 8.5.4 [dcl.init.list], the relevant paragraph is
p. 3. We see that b. 3 is the first match:

"Otherwise, if T is a class type, constructors are considered. [..]
otherwise, the argument list consists of the elements of the
initializer list. The applicable constructors are enumerated
(13.3.1.7) and the best one is chosen through overload resolution
(13.3)."

>From this we can deduce that c'tors with default arguments
are considered according to normal overload deduction rules.

HTH & Greetings from Bremen,

Daniel Kr   gler


--
[ 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                      ]