Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting pointer as template argument: Comeau & MSVC compile, GCC fails

Consider the following code:

template<int* a>
class base {};

int main()
{
    base<(int*)0> test;
    return 0;
}

Both Comeau and MSVC compile this without issues (except for Comeau warning about an unused variable), while GCC fails on the base<(int*)0> test; line, stating

In function `int main()': a casts to a type other than an integral or enumeration type cannot appear in a constant-expression

template argument 1 is invalid

What exactly is it complaining about? And who's right -- should this code compile? It's worth noting that my GCC version is extremely old (3.4.2) so that may have something to do with it. Thanks.

like image 884
GRB Avatar asked Sep 13 '09 15:09

GRB


1 Answers

From a draft standard (emphasis added):

14.1.3 A non-type template-parameter shall have one of the following (option-
  ally cv-qualified) types:
  ...
  --pointer to object, accepting an address constant  expression  desig-
    nating a named object with external linkage,
  ...

Apparently, it's not legal to instantiate a template with a null pointer, as a null pointer doesn't point to a "named object with external linkage".

like image 126
Managu Avatar answered Oct 22 '22 21:10

Managu