REQ HowTo: Passing class specific char ptr through template declaration

D

Dave Miller

How can you pass a string through a template parameter? I am trying
to avoid using the stream objects and need to pass in class specific
information at creation.

Any information would be helpful. Thanks,

dmille

i.e.

template<class T, const char *FMT>
class Foo
{
private :
T _x ;

public :
foo() {}
short scan_it( const char *src )
{
return sscanf( src, FMT, &_x ) ;
}
} ;

typedef Foo<long, "%ld"> LongFoo ;

....
LongFoo tmp ;

tmp.scan_it( "192" ) ;
 
V

Victor Bazarov

Dave Miller said:
How can you pass a string through a template parameter? I am trying
to avoid using the stream objects and need to pass in class specific
information at creation.

Any information would be helpful. Thanks,

dmille

i.e.

template<class T, const char *FMT>
class Foo
{
private :
T _x ;

public :
foo() {}
short scan_it( const char *src )
{
return sscanf( src, FMT, &_x ) ;
}
} ;

typedef Foo<long, "%ld"> LongFoo ;

You cannot use string literals as non-type template arguments.
Those things have to have external linkage. You should be able
to do

extern const char *PctLD = "%ld"; // if you need this in
// a header, initialise it
// only once in some source
// file
typedef Foo said:
...
LongFoo tmp ;

tmp.scan_it( "192" ) ;

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top