Reputation: 759
Consider the following code.
template <typename Metadata>
struct S {
double data;
Metadata metadata;
explicit S(double d = 0., Metadata const & m = Metadata()) :
data(d), metadata(m)
{}
};
struct NoDefaultConstructor {
NoDefaultConstructor(int) {}
};
struct PrivateDefaultConstructor {
PrivateDefaultConstructor(int) {}
private:
PrivateDefaultConstructor() {}
};
The following compiles without problem:
S<float> sf;
As expected, the following code samples fail to compile:
S<NoDefaultConstructor> sndc;
S<NoDefaultConstructor> sndc(1);
S<PrivateDefaultConstructor> spdc;
S<PrivateDefaultConstructor> spdc(1);
But what about the following:
S<NoDefaultConstructor> sndc(1, 1);
S<PrivateDefaultConstructor> spdc(1, 1);
It compiles fine with GCC 4.4.1, but what has the standard to say about it? Should I expect an error at template instantiation of S
with NoDefaultConstructor
or PrivateDefaultConstructor
?
More generally, does SFINAE apply on default parameters values?
Thanks.
Upvotes: 3
Views: 396
Reputation: 3166
It should compile fine because there are no need to invoke default constructor when the second parameter is explicitly specified (only conversion constructor from int and copy constructor are invoked).
Note: it is not related to SFINAE
Upvotes: 2