By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 981 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

REQ HowTo: Passing class specific char ptr through template declaration

P: n/a
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" ) ;
Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Dave Miller" <dm******@hotmail.com> wrote...
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<long, PctLD> LongFoo;

...
LongFoo tmp ;

tmp.scan_it( "192" ) ;


Victor
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.