Discussion:
Problem compiling template
(too old to reply)
Strider
2004-01-13 12:50:49 UTC
Permalink
This code works fine for me:

template<int i> class A {};
A<10> a;

This however does not:

template<std::string s> class A{}; // error C2993: 's' : template
parameter can never be initialized
A<"10"> a;

Neither does this:

template<const char * s> class A{};
A<"10"> a; //error C2964: invalid expression as template parameter

I'm using VC6. Can anyone help?

Thanks,

Strider.
tom_usenet
2004-01-13 13:41:13 UTC
Permalink
On Tue, 13 Jan 2004 13:50:49 +0100, "Strider"
Post by Strider
template<int i> class A {};
A<10> a;
template<std::string s> class A{}; // error C2993: 's' : template
parameter can never be initialized
A<"10"> a;
template<const char * s> class A{};
A<"10"> a; //error C2964: invalid expression as template parameter
I'm using VC6. Can anyone help?
Non-type template parameters can only be integral types (int, bool,
short, etc.), enums, references to objects with external linkage,
pointers to objects with external linkage, pointers to functions and
pointers to members. double, std::string, std::complex, etc. can't be
used. This is mostly because template parameters must have a notion of
compile-time value, and double and string, etc. don't have that
concept.

const char* can be used as long as it is a pointer to an object with
external linkage. String literals aren't appropriate values though,
since they don't have external linkage. A global, extern array is
suitable though:

template<const char * s>
class A{};
const char s[] = "10";
A<s> a;

As a final point, if you want to use templates it is definitely worth
upgrading to VC7.1. VC6 has poor template support. Almost any other
compiler (including free ones) would be better for this kind of thing.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Strider
2004-01-13 15:53:48 UTC
Permalink
Thanks for the explanation tom. Unfortunately I'm stuck with VC6 for the
time being (company policy).

This did not compile (VC6):

template<const char * s>
class S3{};
const char sz[] = "10";
S3<sz> a;

error C2970: 'S3' : template argument 's' : invalid address of static
variable 'sz'
: see declaration of 'S3'
: see reference to class template instantiation '?$S3@$1sz' being
compiled

I suspect VC6 to be the problem.

Thanks,

Strider
Post by tom_usenet
On Tue, 13 Jan 2004 13:50:49 +0100, "Strider"
Post by Strider
template<int i> class A {};
A<10> a;
template<std::string s> class A{}; // error C2993: 's' : template
parameter can never be initialized
A<"10"> a;
template<const char * s> class A{};
A<"10"> a; //error C2964: invalid expression as template parameter
I'm using VC6. Can anyone help?
Non-type template parameters can only be integral types (int, bool,
short, etc.), enums, references to objects with external linkage,
pointers to objects with external linkage, pointers to functions and
pointers to members. double, std::string, std::complex, etc. can't be
used. This is mostly because template parameters must have a notion of
compile-time value, and double and string, etc. don't have that
concept.
const char* can be used as long as it is a pointer to an object with
external linkage. String literals aren't appropriate values though,
since they don't have external linkage. A global, extern array is
template<const char * s>
class A{};
const char s[] = "10";
A<s> a;
As a final point, if you want to use templates it is definitely worth
upgrading to VC7.1. VC6 has poor template support. Almost any other
compiler (including free ones) would be better for this kind of thing.
Tom
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Hendrik Schober
2004-01-15 09:19:21 UTC
Permalink
Post by Strider
Thanks for the explanation tom. Unfortunately I'm stuck with VC6 for the
time being (company policy).
template<const char * s>
class S3{};
const char sz[] = "10";
S3<sz> a;
error C2970: 'S3' : template argument 's' : invalid address of static
variable 'sz'
: see declaration of 'S3'
compiled
I suspect VC6 to be the problem.
No. VC7.1 doesn't compile it either:
'S3' : template parameter 's' : 'sz' : an expression involving objects with internal linkage cannot be used as a non-type
argument
FWIW, Comeau agrees with that:
error: a template argument may not reference a non-external entity

However, this

extern const char sz[] = "10";

compiles.
Post by Strider
Thanks,
HTH,
Post by Strider
Strider
[...]
Schobi
--
***@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Strider
2004-01-15 12:48:14 UTC
Permalink
Using:

extern const char sz[] = "10";

also compiles under VC6.

Thanks for the help guys!!!

-- Strider
Post by Hendrik Schober
Post by Strider
Thanks for the explanation tom. Unfortunately I'm stuck with VC6 for the
time being (company policy).
template<const char * s>
class S3{};
const char sz[] = "10";
S3<sz> a;
error C2970: 'S3' : template argument 's' : invalid address of static
variable 'sz'
: see declaration of 'S3'
compiled
I suspect VC6 to be the problem.
'S3' : template parameter 's' : 'sz' : an expression involving objects
with internal linkage cannot be used as a non-type
Post by Hendrik Schober
argument
error: a template argument may not reference a non-external entity
However, this
extern const char sz[] = "10";
compiles.
Post by Strider
Thanks,
HTH,
Post by Strider
Strider
[...]
Schobi
--
I'm Schobi at suespammers dot org
"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Greg Comeau
2004-01-27 00:07:38 UTC
Permalink
Post by Hendrik Schober
Post by Strider
Thanks for the explanation tom. Unfortunately I'm stuck with VC6 for the
time being (company policy).
template<const char * s>
class S3{};
const char sz[] = "10";
S3<sz> a;
error C2970: 'S3' : template argument 's' : invalid address of static
variable 'sz'
: see declaration of 'S3'
compiled
I suspect VC6 to be the problem.
'S3' : template parameter 's' : 'sz' : an expression involving objects with internal linkage cannot be used as a non-type
argument
error: a template argument may not reference a non-external entity
However, this
extern const char sz[] = "10";
compiles.
Sounds right. For anybody interesed in some similar talk, have a peek
at http://www.comeaucomputing.com/techtalk/templates/#stringliteral
--
Greg Comeau/Comeau C++ 4.3.3 for $43.30 - 43.3 day limited time offer
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Loading...