CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2001
    Location
    Oslo, Norway
    Posts
    610

    ANSI defined template design wont compile --->

    From MCVC7.0 compiler error description:

    invalid template argument 'number', constant expression expected

    The template argument does not match the template declaration; a constant expression should appear within the angle brackets. Variables are not allowed as template actual arguments. Check the template definition to find the correct types.

    The following sample generates C2975:

    // C2975.cpp
    template <char *P>
    class x
    {
    char * f()
    {
    return P;
    }
    };

    x<"abc"> *p = 0; // C2975 addr of object with internal linkage


    Just what in all **** dont it like ? there is a simple const char design-time constant "abc" passed as a parameter to template.

    I tried to compile this and it wont of course.... How to overcome this stupid glitch ? Isnt it a standard C++ ?!?!

  2. #2
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    I know less about templates than you do but using VC 5 the following compiles:
    Code:
    template <char *P>
    class x {
    	char * f() {
    	return P;
    	}
    };
    Code:
    char xp[]="abc";
    x<xp> *p = 0;
    Isn't there supposed to be a semicolon after a class? If so then perhaps VC 7 is being more accurate; VC 5 compiles with or without a semicolon.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449
    Hello Amn,

    The Comeau compiler also rejects the syntax of passing a string literal as a template argument.

    Here is the error:
    "ComeauTest.c", line 12: error: a template argument may not reference a
    non-external entity
    x<"abc"> p;
    The relevant section in the ANSI / ISO C++ standard is 14.3.2.2
    2 [Note: A string literal (2.13.4) is not an acceptable template argument
    because a string literal is an object
    with internal linkage.

    [Example:
    template<class T, char* p> class X {
    // ...
    X();
    X(const char* q) { /* ... */ }
    };
    X<int,"Studebaker"> x1; // error: string literal as template argument
    char p[] = "Vivisectionist";
    X<int,p> x2; // OK
    —end example]
    —end note]
    So your code is in error and not the compiler.

    Sam has the correct solution -- create an array of char and use that.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    May 2001
    Location
    Oslo, Norway
    Posts
    610
    Thanks a lot, now i realise how arrogant of me it was to blame the compiler...but hey, when i recall sitting and tweaking for an hour to get it to compile, you know how one gets pretty wired up there...

  5. #5
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Those things happen. I think it is necessary for every good programmer to have experiences like that.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  6. #6
    Join Date
    Aug 2002
    Posts
    78
    I think it is necessary for every good programmer to have experiences like that.
    It took me two days to find this error:

    Code:
    while (some condition);
         {
              do some work;
         }
    The bulk of which time was carving out a small enough portion of a large project, stubbing out dozens of pieces, such that I could load the reduced program into the ancient Quick C text-based debugger (640k RAM limit, yo ho ho and a bottle of rum.)

    What it taught me was to use a machine gun any time a guru says something like:

    Code:
    Gosh golly gewhillikers this is OK and g00d to d00:
    
    for (something;something;else);
    
    
    'causies it savsies one line of vertical whitespace!


    "I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum, the most powerful handgun in the world, and would blow you're head clean off, you've got to ask yourself one question: 'Do I really wanna conserve that one line of whitespace?' Well, do ya...punk?"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured