c ++ 11中首选的初始化方式

CrH*_*her 7 c++ c++11

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)
Run Code Online (Sandbox Code Playgroud)

哪一个使用?萨特说使用:

int i = 0;
auto i = 0;
Run Code Online (Sandbox Code Playgroud)

为什么不:

int i = {0};
auto i = int{0};
Run Code Online (Sandbox Code Playgroud)

在某些情况下我应该摆脱"=":

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}; ...
Run Code Online (Sandbox Code Playgroud)

编辑: 这是我的目标,在我看来,最一致的是:

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;
Run Code Online (Sandbox Code Playgroud)

真人生活的例子:

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);
Run Code Online (Sandbox Code Playgroud)

替代方案是:

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);
Run Code Online (Sandbox Code Playgroud)

如果不使用大括号那么它很丑陋或容易出错:

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.
Run Code Online (Sandbox Code Playgroud)

Edw*_*ard 8

对于简单的事情,例如int在你的例子中,我同意这一点

int i=0; 
Run Code Online (Sandbox Code Playgroud)

可能是最常被理解的(在程序员中),但使用大括号初始化有一些优点,对我而言,它更受欢迎.例如

int i = 3.99;    // i gets 3; no warning, no error
int i{3.99};     // i gets 3; warning: "narrowing conversion"
Run Code Online (Sandbox Code Playgroud)

它有助于编写更多无错误的代码,因此在我看来这是一种更好的方法.

混合它auto更危险.我通常auto只用于:

  • 范围for for循环中的临时变量(例如for (const auto &n : mycollection))
  • 简化命名lambda的声明
  • 当我明确地使用它们时(而不是range-for)用于迭代器实例
  • 模板化代码,这样做可以避免创建冗长的typedef

  • 欢迎来到初始化列表/统一初始化地狱. (4认同)

Ded*_*tor 5

有一些错误的推导:

auto i{0}; // [comment omitted]
int i();
Run Code Online (Sandbox Code Playgroud)

第一个定义istd::initializer_list<int>.
第二个声明一个名为“i返回”int并且没有参数的 extern 函数。

经验法则:

  • auto在可以节省打字并且类型或其行为显而易见的地方使用。例子:

    auto x = new mymegathingy;
    auto y = container.begin();
    auto z = filestream.seekoff(0, basic_ios::curr);
    
    Run Code Online (Sandbox Code Playgroud)
  • 在可行的地方使用赋值(当 lhs 和 rhs 具有不同类型时,任何当前编译器都会优化潜在的临时值)。

    int i = 0;
    int* i = 0; // For many types passing `nullptr` is better.
    
    Run Code Online (Sandbox Code Playgroud)
  • 在赋值不起作用的情况下使用通用初始化语法。

    std::vector<int> i = {1,2,3};
    auto i = new int[]{1,2,3};
    
    Run Code Online (Sandbox Code Playgroud)
  • 您可能希望使用直接构造函数调用,其中至少给出一个明显非类型的参数,以避免大括号:

    int i(0);
    
    Run Code Online (Sandbox Code Playgroud)

请注意,使用通用初始化语法进行初始化会与 混合不好auto,我们得到std::initializer_list<>

auto i{0};
Run Code Online (Sandbox Code Playgroud)

只要不传递至少一个明显的非类型参数,就避免使用旧式 init,否则您可能会无意中声明一个函数:

int i();
Run Code Online (Sandbox Code Playgroud)