Topic: Weird Template Issue w/ non-type params.


Author: scherrey@proteus-tech.com
Date: 1995/05/16
Raw View
I'm trying to implement an ObjectID class template that takes a const char*
argument rather than a type argument:

template < const char* Name >
class ObjectID
{
public:
 ObjectID( void ) { ID = 0; }
 virtual ~ObjectID( void ) {;}
 ObjectID& newID( void ) { ID = ++NextID; return *this; }
private:
 unsigned long ID;
 static unsigned long NextID;
 static const char* ClassName;
};

template < const char* Name > ObjectID< Name >::ClassName = Name;   // ERROR?!?

So in my code I try:

typedef ObjectID< "MyClassName" > MyClass;

And I get an error "Template argument must be a constant expression."

 How can I accomplish what it is I'm trying to do here?!?!? I don't know if
this is a language issue, a compiler (BCOS2) issue, or a stupid programmer issue!
All comments and suggestions are greatly appreciated!

    thanx & later,

 Ben Scherrey
 Proteus Technologies, Inc.






Author: jaf3@ritz.cec.wustl.edu (John Andrew Fingerhut)
Date: 1995/05/17
Raw View
In article <3pa24b$hh0@keystone.intergate.net>,
 <scherrey@proteus-tech.com> wrote:
:I'm trying to implement an ObjectID class template that takes a const char*
:argument rather than a type argument:
:
:template < const char* Name >
:class ObjectID
:{
:public:
: ObjectID( void ) { ID = 0; }
: virtual ~ObjectID( void ) {;}
: ObjectID& newID( void ) { ID = ++NextID; return *this; }
:private:
: unsigned long ID;
: static unsigned long NextID;
: static const char* ClassName;
:};
:
:template < const char* Name > ObjectID< Name >::ClassName = Name;   // ERROR?!?
:
:So in my code I try:
:
:typedef ObjectID< "MyClassName" > MyClass;
:
:And I get an error "Template argument must be a constant expression."
:
: How can I accomplish what it is I'm trying to do here?!?!? I don't know if
:this is a language issue, a compiler (BCOS2) issue, or a stupid programmer issue!
:All comments and suggestions are greatly appreciated!
:
:    thanx & later,
:
: Ben Scherrey
: Proteus Technologies, Inc.
:

You can't use a string literal as a template argument [14.8].  You can,
however, create a char * variable to create your template class with.
For example,

 char *p = "MyClassName";
 ObjectID<p> MyClassNameObj;

Without this restriction, the two objects in the following example *might* be
of different types because the string literals could occupy different
locations in memory:

 ObjectID<"MyClass"> o1;
 ObjectID<"MyClass"> o2;

In fact, the string literals would have to occupy different memory if the
declarations existed in different source modules.

--
Stephen Gevers
sg3235@shelob.sbc.com





Author: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Date: 1995/05/18
Raw View
In article <3pd36i$shp@ritz.cec.wustl.edu> jaf3@ritz.cec.wustl.edu
(John Andrew Fingerhut) writes:

|> In article <3pa24b$hh0@keystone.intergate.net>,
|>  <scherrey@proteus-tech.com> wrote:
|> :I'm trying to implement an ObjectID class template that takes a const char*
|> :argument rather than a type argument:
|> :
|> :template < const char* Name >
|> :class ObjectID
|> :{
|> :public:
|> : ObjectID( void ) { ID = 0; }
|> : virtual ~ObjectID( void ) {;}
|> : ObjectID& newID( void ) { ID = ++NextID; return *this; }
|> :private:
|> : unsigned long ID;
|> : static unsigned long NextID;
|> : static const char* ClassName;
|> :};
|> :
|> :template < const char* Name > ObjectID< Name >::ClassName = Name;   // ERROR?!?
|> :
|> :So in my code I try:
|> :
|> :typedef ObjectID< "MyClassName" > MyClass;
|> :
|> :And I get an error "Template argument must be a constant expression."
|> :
|> : How can I accomplish what it is I'm trying to do here?!?!? I don't know if
|> :this is a language issue, a compiler (BCOS2) issue, or a stupid programmer issue!
|> :All comments and suggestions are greatly appreciated!
|> :
|> :    thanx & later,
|> :
|> : Ben Scherrey
|> : Proteus Technologies, Inc.
|> :

|> You can't use a string literal as a template argument [14.8].  You can,
|> however, create a char * variable to create your template class with.
|> For example,

|>  char *p = "MyClassName";
|>  ObjectID<p> MyClassNameObj;

Does this really work?  I don't think it should.

The template argument must be a constant expression; p is *not* a
constant expression.

I think that the following should work:

 char            n[] = "MyClassName" ;
 ObjectID<n>     myClassNameObject ;

Note that `n' must have external linkage; i.e.: it may not be static,
and must be declared outside of any function body.
--
James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils en informatique industrielle --
                              -- Beratung in industrieller Datenverarbeitung