Home > Net >  String name of template function
String name of template function

Time:01-28

I know you can use the preprocessor to get the textual name of a standard function at compile time via __func__

My question is, is there any way to get the textual name of a template function including its specific implementation details (mangled is better than nothing)?

For example,

template <typename T>
void myFunction() {
    std::cout << //Function name here;

};

Output:

myFunction<int>(); -> "myFunction<int>" (or mangled name)
myFunction<char>(); -> "myFunction<char>" (or mangled name)

CodePudding user response:

For use with g you can use __PRETTY_FUNCTION__.

So for

#include <iostream>

template<typename T>
void myFunction() {
    std::cout << __PRETTY_FUNCTION__ << '\n';
};

int main() {
    myFunction<int>(); 
    myFunction<char>();
}

I get

void myFunction() [with T = int]
void myFunction() [with T = char]
  •  Tags:  
  • Related