why does `vector<int> v{{5,6}};` work? I thought only a single pair {} was allowed?

女生的网名这么多〃 提交于 2019-12-06 19:51:35

问题


Given a class A with two constructors, taking initializer_list<int> and initializer_list<initializer_list<int>> respectively, then

A v{5,6};

calls the former, and

A v{{5,6}};

calls the latter, as expected. (clang3.3, apparently gcc behaves differently, see the answers. What does the standard require?)

But if I remove the second constructor, then A v{{5,6}}; still compiles and it uses the first constructor. I didn't expect this. I thought that A v{5,6} would be the only way to access the initializer_list<int> constructor.

(I discovered this while playing around with std::vector and this question I asked on Reddit, but I created my own class A to be sure that it wasn't just a quirk of the interface for std::vector.)


回答1:


I think this answer might be relevant.

Yes, this behaviour is intended, according to §13.3.1.7 Initialization by list-initialization

When objects of non-aggregate class type T are list-initialized (8.5.4), overload resolution selects the constructor in two phases:

— Initially, the candidate functions are the initializer-list constructors (8.5.4) of the class T and the argument list consists of the initializer list as a single argument.

— If no viable initializer-list constructor is found, overload resolution is performed again, where the candidate functions are all the constructors of the class T and the argument list consists of the elements of the initializer list.

In gcc I tried your example. I get this error:

error: call of overloaded 'A(<brace-enclosed initializer list>)' is ambiguous

gcc stops complaining if I use three sets of brace. i.e.:

#include <iostream>
#include <vector>
#include <initializer_list>

struct A {
    A (std::initializer_list<int> il) { 
        std::cout << "First." << std::endl;
    }
    A (std::initializer_list<std::initializer_list<int>> il) { 
        std::cout << "Second." << std::endl;
    }
};

int main()
{
    A a{0}; // first
    A a{{0}}; // compile error
    A a2{{{0}}}; // second
    A a3{{{{0}}}}; // second
}

In an attempt to mirror the vector's constructors, here are my results:

#include <iostream>
#include <vector>
#include <initializer_list>

struct A {
    A (std::initializer_list<int> il) { 
        std::cout << "First." << std::endl;
    }
    explicit A (std::size_t n) {
        std::cout << "Second." << std::endl;
    }
    A (std::size_t n, const int& val) {
        std::cout << "Third." << std::endl;
    }
    A (const A& x) {
        std::cout << "Fourth." << std::endl;
    }
};

int main()
{
    A a{0};
    A a2{{0}};
    A a3{1,2,3,4};
    A a4{{1,2,3,4}};
    A a5({1,2,3,4});
    A a6(0);
    A a7(0, 1);
    A a8{0, 1};
}

main.cpp:23:10: warning: braces around scalar initializer

    A a2{{0}};

         ^~~

1 warning generated.

First.
First.
First.
First.
First.
Second.
Third.
First.


来源:https://stackoverflow.com/questions/20714273/why-does-vectorint-v5-6-work-i-thought-only-a-single-pair-was-allow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!