std::initializer_list 构造函数中的转换

std::initializer_list conversion in constructors

我是 C++11 的新手,我想知道这段代码在内部是如何工作的:

class MyClass
{
    public:
        MyClass(int a, double b) {
            cout << "ctor()" << endl;
        }
};

int main()
{
    MyClass i1{4, 7};
    return 0;
}

我对new initializer list的理解是,它是在代码中通过特殊语法{ .... }构建的classstd::initializer_list。那么这个由 {4, 7} 创建的 class 实例如何在内部转换为适合 MyClass 的构造函数的形式?谢谢。

我想事情就是这样发生的。摘自:Explanation of list initialization at cppreference.com

If the previous stage does not produce a match, all constructors of T participate in overload resolution against the set of arguments that consists of the elements of the braced-init-list, with the restriction that only non-narrowing conversions are allowed. If this stage produces an explicit constructor as the best match for a copy-list-initialization, compilation fails (note, in simple copy-initialization, explicit constructors are not considered at all)