Reputation: 3943
Assume I have this program, in which I want a bunch of specialized types and no general type. When I compile it, I'd like to get "Error: Int"
instead of "Error: Z"
. I've tried typeof(Z)
and that didn't get me anything - is there a standard trick to this?
template<class Z>
Z derp()
{
static_assert(false, "Error: Z");
}
template <>
double derp<double>()
{
return 5;
}
int main()
{
double b = derp<double>();
int r = derp<int>();
return 0;
}
Upvotes: 3
Views: 576
Reputation: 170203
Strictly speaking, your program is ill-formed no diagnostic required on account of that static assertion violating [temp.res]/8. But that is easily fixable. We can just define the primary template as deleted:
template<class Z>
Z derp() = delete;
template<>
double derp<double>()
{
return 5;
}
int main()
{
double b = derp<double>();
int r = derp<int>();
return 0;
}
Now the program is well formed, unless the primary specialization is actually called. Most compilers will also give the error message you want. Here's Clang 5.0:
prog.cc:14:13: error: call to deleted function 'derp'
int r = derp<int>();
^~~~~~~~~
prog.cc:2:3: note: candidate function [with Z = int] has been explicitly deleted
Z derp() = delete;
^
Upvotes: 3