This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Initializer List Ctor
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Julius Witte <julius dot witte at iwr dot uni-heidelberg dot de>
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Tue, 5 Sep 2017 16:03:22 +0100
- Subject: Re: Initializer List Ctor
- Authentication-results: sourceware.org; auth=none
- References: <22348dc0-35c4-3e36-00c0-d02c858759cc@iwr.uni-heidelberg.de>
On 5 September 2017 at 14:06, Julius Witte wrote:
> In a class such as this one:
>
> #include <initializer_list>
> class A
> {
> public:
> A(int i, double d){}
> A(std::initializer_list<double> l){}
> };
>
> why an instantiation with curly braces as this one
>
> int main()
> {
> A a{2,2.} ;
> }
>
> shall default to the initializer list constructor?
Because that's what the C++ standard says must happen. GCC just
follows the standard.
> I understand why a call like "A a{2.,2.}" would default to it,
>
> but in my eyes it would make more sense to default
>
> to the first constructor when the types match it exactly.
The rule is that a non-empty braced-init-list will prefer an
initializer-list constructor if it's viable. {2, 2.} can be converted
to std::initializer_list<double> so that's what happens.