【发布时间】:2018-08-15 09:49:29
【问题描述】:
考虑以下代码,它是一个简单的类,其构造函数采用默认值作为参数。
// 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;
}
构造函数的两个版本是等价的(对于任何T,总是会产生相同的结果),还是它们不同?
如果它们不同,您能否提供一个T 的示例,这两者会导致不同的结果?
【问题讨论】:
-
为什么这么冗长?我是否错过了什么或者问题基本上是关于
T t = {};和T t = T();之间的区别?
标签: c++ c++11 default-constructor default-arguments list-initialization