相关疑难解决方法(0)

构造对象时,C ++中括号和花括号之间有什么区别

(){}构造对象之间有什么区别?

我认为{}应该只支持with initializer_list或array,但是当我在snip之下运行时,我感到困惑。

#include <iostream>
using namespace std;
struct S {
    int v=0;
    S(int l) : v(l) {
    }
};


int main()
{
    S s1(12); // statement1
    S s2{12}; // statement2
    cout << s1.v << endl;
    cout << s2.v << endl;
}
Run Code Online (Sandbox Code Playgroud)

statement1是正确的,因为这()是构造对象的基本语法。

我希望statement2将编译失败。我认为{}只能用于数组或initializer_list类型。但是实际结果可以完美编译而不会出错。

我怎么了

c++ initialization c++11

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

标签 统计

c++ ×1

c++11 ×1

initialization ×1