Click to See Complete Forum and Search --> : ANSI defined template design wont compile --->


Amn
September 22nd, 2002, 01:14 PM
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++ ?!?!

Sam Hobbs
September 22nd, 2002, 01:46 PM
I know less about templates than you do but using VC 5 the following compiles:template <char *P>
class x {
char * f() {
return P;
}
};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.

Paul McKenzie
September 23rd, 2002, 01:08 PM
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

Amn
September 27th, 2002, 10:19 AM
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...

Sam Hobbs
September 27th, 2002, 11:36 AM
Those things happen. I think it is necessary for every good programmer to have experiences like that.

Gorgor
October 2nd, 2002, 12:04 PM
I think it is necessary for every good programmer to have experiences like that.

It took me two days to find this error:


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:


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