As int()
and int{}
are constant expressions of value equal to 0
, I thought they are equivalent and interchangeable, thus compilers must treat them equally. For example,
int a[0]; //error: zero-sized array not allowed in ISO C++
int b[int()]; //error: zero-sized array not allowed in ISO C++
int c[int{}]; //error: zero-sized array not allowed in ISO C++
But it seems there are some corner cases where they're not interchangeable.
When initializing a pointer:
int *p = 0; //ok int *q = int(); //error - by clang only int *r = int{}; //error - by gcc and clang both
See GCC and Clang messages. I suspect this is a bug in both compilers, as I expect them to be interchangeable in this context, but I would be glad to be proven wrong. :-)
When passing to class template:
template<int N> struct X{}; X<0> x1; //ok X<int{}> x2; //ok (same as X<0>) X<int()> x3; //error
I find the syntax
X<int()>
quite familiar as I've seen (and probably used) the similar syntax before, such as instd::function<int()>
, the template argumentint()
is expected to be function type (instead of0
) taking no argument and returningint
. But I want to know the section of the spec which says in this contextint()
is to be treated as function type and is not equivalent toint{}
which is always0
.