26 September 2023

C++ 核心指南目录

“Use {} rather than () within templates to avoid ambiguities”

理由

() 很容易导致语法模糊。

例子

template<typename T, typename U>
void f(T t, U u)
{
    T v1(T(u));    // mistake: oops, v1 is a function not a variable
    T v2{u};       // clear:   obviously a variable
    auto x = T(u); // unclear: construction or cast?
}

f(1, "asdf"); // bad: cast from const char* to int

强化

  • 标记通过 () 初始化的地方
  • 标记函数风格的类型转换