Declare type in function template based on class t type

J

Jim Langston

Following does not compile for good reason(s).

template <class t>
GLvoid jglPrint( const jglFont& font, t text, jglVertex2& position ) {

// ...
if ( typeid( t ) == typeid( const char* ) )
std::basic_string <char> buffer( text );
else if ( typeid( t ) == typeid( const wchar_t * ) )
std::basic_string <wchar_t> buffer(text);

Basically I am accepting a few types for text anything that resembles text.
char*, const char*, wchar_t*, std::basic_string<wchar_t>, etc..

I want to declare my buffer containing the same basic type as text (char,
wchar_t, u16, etc..). I can't quite figure out the syntax. I'm fairly sure
it can be done but I hope in some elegant manner. Any suggestions?
 
A

Alf P. Steinbach /Usenet

* Jim Langston, on 21.10.2010 08:09:
Following does not compile for good reason(s).

template <class t>
GLvoid jglPrint( const jglFont& font, t text, jglVertex2& position ) {

// ...
if ( typeid( t ) == typeid( const char* ) )
std::basic_string <char> buffer( text );
else if ( typeid( t ) == typeid( const wchar_t * ) )
std::basic_string <wchar_t> buffer(text);

Basically I am accepting a few types for text anything that resembles text.
char*, const char*, wchar_t*, std::basic_string<wchar_t>, etc..

I want to declare my buffer containing the same basic type as text (char,
wchar_t, u16, etc..). I can't quite figure out the syntax. I'm fairly sure it
can be done but I hope in some elegant manner. Any suggestions?

A type traits class, that is, a class template with a typedef, specialized for
each relevant 't'.

Cheers & hth.,

- Alf
 
J

Jim Langston

Alf P. Steinbach /Usenet said:
* Jim Langston, on 21.10.2010 08:09:

A type traits class, that is, a class template with a typedef, specialized
for each relevant 't'.

Thanks, this seems to work.

template <typename T> class Basetype {
private:
typedef void Type;
};
template<> class Basetype<const wchar_t*> {
public:
typedef wchar_t Type;
};
template<> class Basetype<const char*> {
public:
typedef char Type;
};

std::basic_string<Basetype<T>::Type> buffer( text );
 

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
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top