C++ Get Name of Type in Template

C++ Get name of type in template

Jesse Beder's solution is likely the best, but if you don't like the names typeid gives you (I think gcc gives you mangled names for instance), you can do something like:

template<typename T>
struct TypeParseTraits;

#define REGISTER_PARSE_TYPE(X) template <> struct TypeParseTraits<X> \
{ static const char* name; } ; const char* TypeParseTraits<X>::name = #X


REGISTER_PARSE_TYPE(int);
REGISTER_PARSE_TYPE(double);
REGISTER_PARSE_TYPE(FooClass);
// etc...

And then use it like

throw ParseError(TypeParseTraits<T>::name);

EDIT:

You could also combine the two, change name to be a function that by default calls typeid(T).name() and then only specialize for those cases where that's not acceptable.

how to print template typename in c++?

You can use typeid(T).name() to get the raw string of the template parameter:

#include <boost/numeric/conversion/cast.hpp>
#include <stdexcept>
#include <typeinfo>

template <typename Source, typename Target>
Target numeric_cast(Source src)
{
try
{
// calling boost numeric_cast here
}
catch(boost::numeric::bad_numeric_cast& e)
{
throw (std::string("numeric_cast failed, fromType: ") +
typeid(Source).name() + " toType: " + typeid(Target).name());
}
}

Demo.

Please note that the string literal "numeric_cast failed, fromType:" should be std::string type to support '+' operator.

C++, templates: get type of the item

You can typedef the template parameter T in the struct and use std::list::value_type to get the type stored in the struct AB if you don't use C++11:

#include<list>
#include<iostream>

template <typename T>
struct AB
{
T a, b;

typedef T value_type;

AB( )
:
a (0),
b (0)
{}

};


template <typename T>
struct ABList
{
typedef std::list < AB <T> > Type;
typedef T Type2;
};

template <typename List>
void test ( List & l )
{
typename List::iterator i_l = l.begin();

typename List::value_type::value_type& Listval = i_l->a;

std::cout << Listval << std::endl;
}

template <typename List>
void test (List l, typename List::value_type::value_type val )
{
std::cout << "test" << std::endl;
}

int main(int argc, char* argv[])
{
ABList <double> ::Type intervals;

double x = 7.0;

test(intervals, x);

return 0;
}

Is it possible to get the name of a type alias in C++?

No, you cannot, barring stringifying macros (which has nothing to do with type system).

There are ways to do strong typedefs, but they generate new types.

Note that the name on a typeid need not be human readable or even unique.

C++ Get name of a template function argument

You can pass the string name of the function into GL_INVOKE, something like this:

template <class F, typename ...Args>
auto GL_INVOKE(const char* fname, F f, Args && ...args) {
std::cout << "[GL_CALL] " << fname << '(';
GL_TRACE_PRINT(std::forward<Args>(args)...);
std::cout << ")\n";
return f(std::forward<Args>(args)...);
}

#define GL_INVOKE_F(N, ...) \
GL_INVOKE(#N, N, ## __VA_ARGS__)

#define GL_CALL(N, ...) \
GL_INVOKE_F(gl##N , ## __VA_ARGS__)

Demo.

In addition, your GL_TRACE_PRINT can be implemented using C++17 fold expression, which will be much more concise.

template <class... Args>
void GL_TRACE_PRINT(Args const&... args) {
((std::cout << args << ","), ...);
}

Is it possible to get the name of a function passed to a template?

No. typeid only does RTTI for polymorphic objects (i.e. objects of class type, with virtual functions). For all other types, it'll just give you information about the static type, in this case a function pointer type. (typeid for polymorphic objects leverages the vtable to do its work, but there's no vtable-like thing attached to functions.)

For debugging/tracing of this sort, you'll need to use platform-specific debugging utilities.

Getting parameter type of function with templates

You can create a function traits with partial specialization:

template <auto func, std::size_t I>
struct param_type;

template <typename Ret, typename... Args, Ret (*func)(Args...), std::size_t I>
struct param_type<func, I>
{
using type = std::tuple_element_t<I, std::tuple<Args...>>;
};

// C-ellipsis version aka printf-like functions
template <typename Ret, typename... Args, Ret (*func)(Args..., ...), std::size_t I>
struct param_type<func, I>
{
using type = std::tuple_element_t<I, std::tuple<Args...>>;
};

Demo



Related Topics



Leave a reply



Submit