#include <iostream>
#include <vector>
struct S {
//std::vector<int> ns(1); //ERROR!
std::vector<int> ns = std::vector<int>(1);
};
int main() {
S s;
std::cout << (s.ns[0] = 123) << std::endl;
return 0;
}
使用括号初始化可能会导致错误。它的目的是什么?
#include <iostream>
#include <vector>
struct S {
//std::vector<int> ns(1); //ERROR!
std::vector<int> ns = std::vector<int>(1);
};
int main() {
S s;
std::cout << (s.ns[0] = 123) << std::endl;
return 0;
}
std::vector<int> ns();
是一个函数声明。以下内容不是函数声明:
std::vector<int> ns{};
std::vector<int> ns = std::vector<int>();
T t(args...);
禁止使用 disallowed,这避免了最令人烦恼的解析问题的重复发生。
T t();
而不是T t;
? - user3810155T
是一个内置类型,或者是一个具有内置成员的聚合体,则会有所不同:值初始化会导致零初始化,而默认初始化则不会。 - juanchopanza