Consider:
void f() {
return 5;
}
The above will raise errors. But why not this?:
template <typename = void> void f() {
return 0;
}
I’m compiling with gcc-4.5.1. Why does it make a difference using templates such that I wouldn’t receive errors from doing the same illegal return statement as a non-template function?. The only setback I get is that I can’t call the function (i.e f()) without getting:
error: return-statement with a value, in function returning 'void'
But still, what could be the reason that I am able to define a return statement for a void function template?
Here is the code I have:
template <typename = void> void f() {
return 0;
}
// pass
int main() {
}
The above code will pass despite a presumably illegal return statement in a function returning void.
This is a quality of implementation issue. The particular quote from the standard would be:
That is, your program is ill formed because that template cannot be used to generate any valid specialization, but the compiler is not required to diagnose this. When at a later time you instantiate the template, the compiler must generate the specialization, that specialization is not valid and the compiler complains.
You don’t get an error in the template definition because the compiler is following the no diagnostic required path, i.e. ignoring the problem until it can no longer ignore it in the instantiation.