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 7668615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:24:26+00:00 2026-05-31T15:24:26+00:00

When creating a template function in C++ is there a simple way to have

  • 0

When creating a template function in C++ is there a simple way to have the typename of the template represented as a string? I have a simple test case to show what I’m trying to do (note the code shown does not compile):

#include <stdio.h>
template <typename type>
type print(type *addr) 
{
  printf("type is: %s",type);
}

int main()
{
  int a;
  print(&a);
}

// Would like to print something like:
// type is: int

I think that the typename should be available at compile time when the function is instantiated, but I’m not that familiar with templates and I haven’t seen a way to get the typename as a string.

The reason that I want to do this is for some printf type debugging. I have multiple threads running and stepping through with gdb changes the program behavior. So for some things I want to dump information about which functions were executing. It’s not too important so if the solution is overly complex I would skip adding this information to my logging function. But if there was a simple way to do this it would be useful information to have.

  • 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. Editorial Team
    Editorial Team
    2026-05-31T15:24:27+00:00Added an answer on May 31, 2026 at 3:24 pm

    __PRETTY_FUNCTION__ should solve your problem (at run time at least)

    The output to the program below is:

    asfdasdfasdf test<type>::test() [with type = int]
    asfdasdfasdf test<type>::test() [with type = int]
    asfdasdfasdf test<type>::test() [with type = int]
    asfdasdfasdf test<type>::test() [with type = int]
    asfdasdfasdf test<type>::test() [with type = int]
    asfdasdfasdf test<type>::test() [with type = int]
    asfdasdfasdf void tempFunction() [with type = bool]
    asfdasdfasdf void tempFunction() [with type = bool]
    asfdasdfasdf void tempFunction() [with type = bool]
    asfdasdfasdf void tempFunction() [with type = bool]
    asfdasdfasdf void tempFunction() [with type = bool]
    asfdasdfasdf void tempFunction() [with type = bool]
    !!!Hello World!!!
    

    If you really, really, need the typename as a string, you could hack this (using snprintf instead of printf) and pull the substring after ‘=’ and before ‘]’.

    #include <iostream>
    using namespace std;
    
    template<typename type>
    class test
    {
    public:
    test()
    {
        printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
        printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
        printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
        printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
        printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
        printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
    }
    };
    
    template<typename type>
    void tempFunction()
    {
        printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
        printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
        printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
        printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
        printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
        printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
    }
    
    int main() {
        test<int> test;
    
        tempFunction<bool>();
        cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an RFC, official standard, or template for creating a User Agent string?
Is there any way of creating a function that accepts any version of a
I have a Drupal 6 site with a simple theme I'm creating called NPT.
For creating algorithm template function I need to know whether x or X (and
I got trouble in creating special instance of member template function of non-template class.
I've created a small utility function for string conversion so that I don't have
Hi I need some help in creating a template function (as they are known
Problem Statement: I'm creating a template for multi tiered complicated calculations in MS Excel
I'm looking for some LaTeX template for creating quality output. On R-bloggers I've bumped
I am creating my own custom T4 Template that integrates with an ADO.NET Entity

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.