I don't understand why the following code fails to compile when using constructor-style casting:
template<typename T> void foo(const T& t){}
int main(){
foo(unsigned char(0));
}
The errors are:
error: expected primary-expression before ‘unsigned’
for gcc.error: expected '(' for function-style cast or type construction
for clang
However these three syntaxes are correct:
template<typename T> void foo(const T& t){}
int main(){
// c-style cast
foo((unsigned char)0);
// without unsigned
foo(char(0));
// aliased unsigned char
typedef unsigned char uchar;
foo(uchar(0));
}
So the space in the type is obviously to blame here.
I thought it might be somehow related to our old friend the most vexing parse, so I tried the uniform initialization syntax, which is supposed to get rid of this sort of ambiguities, but no luck:
template<typename T> void foo(const T& t){}
int main(){
foo(unsigned char{0});
}
But still:
error: expected primary-expression before ‘unsigned’
for gcc.error: expected '(' for function-style cast or type construction
for clang
So my question is why is it not allowed to have a type containing a space in function-style casts? It doesn't look ambiguous to me.
note: I know I can write foo<unsigned char>(0)
, but it doesn't answer the question ;)