Thread: Is There a way to check a Template's Type

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    38

    Question Is There a way to check a Template's Type

    Is it possible to cout what kind of type a template is? The following code obviously doesn't work, but something alone these lines?

    Code:
    template <typename T>
    void showType()
    {
        cout << T << endl;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I believe you can use typeid.

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    just make sure you enable RTTI first.

    if you don't want to do that, you can use some template specialisation trickery.

    Code:
    template <typename T>
    void showType(); // don't define this
    
    
    template <>
    void showType<int>()
    {
        cout << "int" << endl;
    }
    
    template <>
    void showType<float>()
    {
        cout << "float" << endl;
    }
    
    template <>
    void showType<char>()
    {
        cout << "char" << endl;
    }
    
    template <>
    void showType<MyClass>()
    {
        cout << "MyClass" << endl;
    }
    obviously that's a lot of work, but you can add showType specialisations when you define a new type.

    It's easier to use RTTI, but some people don't like it.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    38
    What I ened up doing is:

    Code:
    template <typename T>
    void showType()
    {
        cout << typeid(T).name() << endl;
    }
    Works great for what I need.

    Thanks,

    Matt

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    But may not have the desirable effect on other compilers. Keep in mind the result of name() is implementation dependent and the resulting string may not fit into your needs.

    If however, you don't plan to make this portable across different compilers, it shouldn't make much of a difference.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    38
    Hmmm Well I am developing in VS 2005, Windows XP.... But this will also need to work under SUSE 10.1, with the gcc compiler.

    Any Known issues with name() there?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It doesn't necessarily return a very user-friendly value, and the value it does return will be different if you move platforms. But for any one particular platform the name will be consistent.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MFC check box question
    By MyglyMP2 in forum Windows Programming
    Replies: 2
    Last Post: 03-09-2009, 05:47 PM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:35 PM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM