Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 138997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:21:22+00:00 2026-05-11T07:21:22+00:00

I want to get the string name (const char*) of a template type. Unfortunately

  • 0

I want to get the string name (const char*) of a template type. Unfortunately I don’t have access to RTTI.

template< typename T > struct SomeClass {     const char* GetClassName() const { return /* magic goes here */; } } 

So

SomeClass<int> sc; sc.GetClassName();   // returns 'int' 

Is this possible? I can’t find a way and am about to give up. Thanks for the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:21:23+00:00Added an answer on May 11, 2026 at 7:21 am

    No and it will not work reliable with typeid either. It will give you some internal string that depends on the compiler implementation. Something like ‘int’, but also ‘i’ is common for int.

    By the way, if what you want is to only compare whether two types are the same, you don’t need to convert them to a string first. You can just do

    template<typename A, typename B> struct is_same { enum { value = false }; };  template<typename A> struct is_same<A, A> { enum { value = true }; }; 

    And then do

    if(is_same<T, U>::value) { ... } 

    Boost already has such a template, and the next C++ Standard will have std::is_same too.

    Manual registration of types

    You can specialize on the types like this:

    template<typename>  struct to_string {     // optionally, add other information, like the size     // of the string.     static char const* value() { return 'unknown'; } };  #define DEF_TYPE(X) \     template<> struct to_string<X> { \         static char const* value() { return #X; } \     }  DEF_TYPE(int); DEF_TYPE(bool); DEF_TYPE(char); ... 

    So, you can use it like

    char const *s = to_string<T>::value(); 

    Of course, you can also get rid of the primary template definition (and keep only the forward declaration) if you want to get a compile time error if the type is not known. I just included it here for completion.

    I used static data-members of char const* previously, but they cause some intricate problems, like questions where to put declarations of them, and so on. Class specializations like above solve the issue easily.

    Automatic, depending on GCC

    Another approach is to rely on compiler internals. In GCC, the following gives me reasonable results:

    template<typename T> std::string print_T() {     return __PRETTY_FUNCTION__; } 

    Returning for std::string.

    std::string print_T() [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]

    Some substr magic intermixed with find will give you the string representation you look for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I would suggest its a usage dependency relationship. See here… May 11, 2026 at 1:53 pm
  • added an answer I found the solution - Linq for Sharepoint how cool… May 11, 2026 at 1:53 pm
  • added an answer #1 Use this instead (note the has selector): var selections=$('.item:has(>.quantity>:text[value!=''])');… May 11, 2026 at 1:53 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.