相关疑难解决方法(0)

如果copy-list-initialization允许显式构造函数会出现什么问题?

在C++标准§13.3.1.7[over.match.list]中,陈述如下:

在copy-list-initialization中,如果explicit选择了构造函数,则初始化是错误的.

这就是为什么我们不能这样做的原因,例如:

struct foo {
    // explicit because it can be called with one argument
    explicit foo(std::string s, int x = 0);
private:
    // ...
};

void f(foo x);

f({ "answer", 42 });
Run Code Online (Sandbox Code Playgroud)

(注意,这里发生的不是转换,即使构造函数是"隐式的"也不会是一个.这是一个foo对象直接使用它的构造函数初始化.除此之外std::string,这里没有转换.)

这对我来说似乎完全没问题.隐式转换不会让我感到困惑.

如果{ "answer", 42 }可以初始化其他东西,编译器不会背叛我并做错事:

struct bar {
    // explicit because it can be called with one argument
    explicit bar(std::string s, int x = 0);
private:
    // ...
};

void f(foo x);
void f(bar x); …
Run Code Online (Sandbox Code Playgroud)

c++ explicit-constructor initializer-list c++11

38
推荐指数
1
解决办法
3926
查看次数