Consider the following code, of a simple class with a constructor taking an argument with a default value.
// 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;
}
Are the two versions of the constructor equivalent (will always produce the same result for any T
), or they are different?
If they are different, could you provide an example of T
for which the two would lead to different results?
T t = {};
andT t = T();
? – Guyenne