6

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.

7
  • FYI: with gcc 4.4.1: test.cpp:6: error: a cast to a type other than an integral or enumeration type cannot appear in a constant-expression test.cpp:6: error: template argument 1 is invalid test.cpp:6: error: invalid type in declaration before ';' token
    – jdehaan
    Sep 13 '09 at 15:45
  • Thanks jdehaan, so we know this has nothing to do with my version.
    – GRB
    Sep 13 '09 at 15:45
  • Ur welcome. I tried modifying the code a bit to make it compile. Slight variations didn't clarify the problem...
    – jdehaan
    Sep 13 '09 at 15:50
  • 3
    The defect report at open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#354 provides insight. It seems that the comeau crew thought the Standard is too restrictive in that point and allowed it, well knowing that it would be resolved and allowed in the Standard later on (2005) anyway. Of course, the defect reporter John Spicer works for comeau. xD Sep 13 '09 at 17:39
  • Nice, thanks for that link litb, shows exactly what I'm trying to do :D
    – GRB
    Sep 13 '09 at 17:41
8

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".

7
  • Related SO: stackoverflow.com/questions/275871/…
    – Managu
    Sep 13 '09 at 16:02
  • 1
    +1! I tried by giving a pointer to an int as parameter and following error came up (better than the quite cryptic one before) error: '& a' is not a valid template argument of type 'int*' because 'a' does not have external linkage
    – jdehaan
    Sep 13 '09 at 16:13
  • 1
    This seems promising, but I don't think this answers why GCC and Comeau differ. For example, if I change to base<(int*)1>, both Comeau and GCC fail to compile. Additionally, if this requirement is from a 'draft', well, my GCC fails to compile it yet it's 4 years old, so if this was added to the standard during that time, then it wouldn't be that requirement that's making GCC choke.
    – GRB
    Sep 13 '09 at 16:47
  • 1
    People often quote from the draft because the standard itself is not available for free. There shouldn't be any significant differences between a final draft and the standard text, however. There hasn't been any new standard since 2003 (which was minor corrections), so your version of GCC is certainly younger than that requirement. The error message also sounds as if the compiler knew exactly what it is talking about. I'm tempted to think it is correct.
    – UncleBens
    Sep 13 '09 at 17:30
  • 3
    To clear it up though, the cited text is from a pre-standard 1996 draft. The Standard ('03) has there: "— pointer to object or pointer to function" and elsewhere it says that the argument should be "— the address of an object or function with external linkage,". So stricly according to '03 comeau is wrong, although being wrong on purpose, "conforming" to the yet-to-be-released c++0x instead. Sep 13 '09 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.