默认参数和空列表初始化

Vin*_*ent 4 c++ default-constructor default-arguments c++11 list-initialization

考虑以下代码,一个简单的类,其构造函数采用带有默认值的参数.

// Version 1
template <class T>
struct object1 {
    using type = T;
    constexpr object1(const type& val = type()): value(val) {}
    type value;
};

// Version 2
template <class T>
struct object2 {
    using type = T;
    constexpr object2(const type& val = {}): value(val) {}
    type value;
};

// Main
int main(int argc, char* argv[]) {
    using type = /* Something */;
    object1<type> x1;
    object2<type> x2;
    auto value1 = x1.value;
    auto value2 = x2.value;
    // Is there certain types for which value1 and value2 will be different?
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

构造函数的两个版本是否等效(对于任何构造函数将始终产生相同的结果T),或者它们是不同的?

如果它们不同,你能提供一个例子,T两者会导致不同的结果吗?

use*_*670 6

不,他们不等同.第二个变体依赖于T的默认构造函数的隐含性:

class foo
{
   public: explicit foo() {}
};

object1<foo> of{}; // ok
object2<foo> of{}; // error
Run Code Online (Sandbox Code Playgroud)

另外我认为从临时调用复制构造函数而不是在没有临时的情况下调用默认构造函数并不是一个好主意.那就是实现单独的构造函数会更好:

template <class T>
struct object1 {
    using type = T;
    constexpr object1(void): value{} {}

    constexpr object1(const type& val): value{val} {}

    type value;
};
Run Code Online (Sandbox Code Playgroud)