Declare type in function template based on class t type

Discussion in 'C++' started by Jim Langston, Oct 21, 2010.

  1. Jim Langston

    Jim Langston Guest

    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?
    Jim Langston, Oct 21, 2010
    #1
    1. Advertising

  2. * 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

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Oct 21, 2010
    #2
    1. Advertising

  3. Jim Langston

    Jim Langston Guest

    "Alf P. Steinbach /Usenet" <> wrote in
    message news:i9olkb$lje$-september.org...
    >* 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'.


    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 );
    Jim Langston, Oct 22, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    562
  2. =?gb2312?B?wfXquw==?=
    Replies:
    10
    Views:
    685
    Victor Bazarov
    Aug 1, 2007
  3. Replies:
    2
    Views:
    657
    Triple-DES
    Feb 26, 2008
  4. claudiu
    Replies:
    2
    Views:
    290
    claudiu
    Apr 1, 2011
  5. claudiu
    Replies:
    3
    Views:
    487
    claudiu
    Apr 1, 2011
Loading...

Share This Page