int i = 0; // (a) Old C style should I use it?
int i{0}; // (b) Brace direct init
int i{}; // (c) Same as (b)
int i = {0}; // (d) as (b)
int i = {}; // (e) as (c)
auto i = 0; // (f) auto = int in this case.
auto i = int{0}; // (g) auto = more specific.
auto i = int{}; // (h) same as above (g)
该使用哪个? Sutter建议使用:
int i = 0;
auto i = 0;
为什么不:
int i = {0};
auto i = int{0};
在某些情况下,我应该去掉“=”符号吗:
int i{0};
auto i{0}; // i is not what some might expect in this case. So I would prefer using "=" everywhere possible like int i = {0}; ...
编辑: 这似乎是我最希望的,也最为一致的:
rectangle w = { origin(), extents() };
complex<double> c = { 2.71828, 3.14159 };
mystruct m = { 1, 2 };
int a[] = { 1, 2, 3, 4 };
vector<int> v = { 1, 2, 3, 4 };
point p = {}; // Default initializes members
int i = {0}; // Checked assembly for this and it's binary the same as int i{0}; could be written also as int i = {};
string s = {""}; // Same as string s = {}; (OR) string s;
现实生活中的例子:
std::string title = { pt.get<std::string>("document.window.title") };
const std::string file = { R"(CoreSettings.xml)" };
int_least64_t currentTick = { 0 }; // (OR) int_least64_t currentTick = {};
bool isRunning = { false }; // (OR) bool isRunning = {};
App* app = { nullptr }; // (OR) App* app = {};
Event event = {};
double detectedFrameRate = { 1000000000.0 / (swapIntervalDeltaCumulative / 20.0) };
double precision = { static_cast<double>(boost::chrono::high_resolution_clock::period::num)
/ boost::chrono::high_resolution_clock::period::den };
auto timeSpan = boost::chrono::duration_cast<boost::chrono::nanoseconds>(nowTime - startTime);
另一种选择是:
std::string title { pt.get<std::string>("document.window.title") };
const std::string file { R"(CoreSettings.xml)" };
int_least64_t currentTick { 0 }; // (OR) int_least64_t currentTick{};
bool isRunning { false }; // (OR) bool isRunning{};
App* app { nullptr }; // (OR) App* app{};
Event event {};
double detectedFrameRate { 1000000000.0 / (swapIntervalDeltaCumulative / 20.0) };
double precision { static_cast<double>(boost::chrono::high_resolution_clock::period::num)
/ boost::chrono::high_resolution_clock::period::den };
auto timeSpan = boost::chrono::duration_cast<boost::chrono::nanoseconds>(nowTime - startTime);
如果不使用大括号,代码将难以阅读或容易出错。
int_least64_t currentTick = 0; // C style - changed this from double to int recently and compiler did not complain so I had something like int_least64_t currentTick = 0.0; ugly!
bool isRunning = false; // C style
App* app = nullptr; // C mixed with C++11 style;
Event event; // might not be initialized by all compilers
int someInt = func(); // func() returns double no error but narrowing.
int i = 0;
。因为auto i{0};
定义的是一个std::initializer_list<int>
,而不是一个整数类型的变量。 - Deduplicatorints
,i = 0
和i = {0}
是相同的。只有当你开始处理其他类型时,这种区别才变得更加相关。我建议不要使用auto i{0}
来表示一个int
,因为现在读者需要检查i
是否被定义为一个类。相反,通过使用auto i = 0
,他们知道他们只是处理一个整数字面量。 - shuttle87int i = (2 * (5 + 27) * 91 * 2) % 4
- SomeWittyUsernamevector<int> v{1,2,3,4};
来初始化元素值),而有时候你则不能这样做(例如vector<int> v(2,42);
来选择另一个构造函数)。总的来说,尽量让代码尽可能易读,这并不意味着要对其施加奇怪的虚假一致性。 - Mike Seymour