相关疑难解决方法(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
查看次数

使用braced-init列表调用显式构造函数:是否含糊不清?

考虑以下:

struct A {
    A(int, int) { }
};

struct B {
    B(A ) { }                   // (1)
    explicit B(int, int ) { }   // (2)
};

int main() {
    B paren({1, 2});   // (3)
    B brace{1, 2};     // (4)
}
Run Code Online (Sandbox Code Playgroud)

建设brace(4)清楚明确地调用(2).在铛,施工paren(3)明确要求(1),其中作为GCC 5.2,它失败编译:

main.cpp: In function 'int main()':
main.cpp:11:19: error: call of overloaded 'B(<brace-enclosed initializer list>)' is ambiguous
     B paren({1, 2});
                   ^
main.cpp:6:5: note: candidate: B::B(A)
     B(A ) …
Run Code Online (Sandbox Code Playgroud)

c++ gcc language-lawyer c++11

21
推荐指数
1
解决办法
906
查看次数