C++ compile-time template arg name?

Started by
13 comments, last by _moagstar_ 11 years, 11 months ago

How would I parse __PRETTY_FUNCTION__ at compile time, to return just the argument type?

It's a moot point anyway, since regardless of how I parse it, it can't return a string literal, I don't think.

I don't think you can. But if you name that function with really pretty name, such like wrongTypeOf(T), it really doesn't matter to include the full function prototype in the error message.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

Advertisement
Which version of gcc are you using? I believe that user defined literals are supported from version 4.7

http://en.cppreference.com/w/cpp/language/user_literal

It may be possible to parse the PRETTY_FUNCTION macro to get the text you require. However I havent tried this and am on a train at the moment so cannot test it myself.

It may be possible to parse the PRETTY_FUNCTION macro to get the text you require.

gcc's __PRETTY_FUNCTION__ isn't a macro, it's a static const local variable.

Which version of gcc are you using? I believe that user defined literals are supported from version 4.7

User-defined literals are in 4.7, but MinGW (the Windows port of GCC that I use) is still at 4.6.

If I was using 4.7, how would that be done?

Something like this?
template<class Arg>
constexpr const char *operator"" TypeName( const char * /* Ignore */)
{
return typeid(Arg).name();
}


"Name of type is: " ## "dummyText"TypeName<Arg>


I'm happy with my current error message ([size=2][color=#008000]""[color=#C0C0C0] [color=#000000]#newTypeName[color=#C0C0C0] [color=#008000]"[color=#C0C0C0] [color=#008000]is[color=#C0C0C0] [color=#008000]not[color=#C0C0C0] [color=#008000]implicitely[color=#C0C0C0] [color=#008000]convertible[color=#C0C0C0] [color=#008000]with[color=#C0C0C0] [color=#008000]"[color=#C0C0C0] [color=#000000]#parentType[color=#C0C0C0] [color=#008000]"-derived[color=#C0C0C0] [color=#008000]classes[color=#C0C0C0] [color=#008000]or[color=#C0C0C0] [color=#008000]strong[color=#C0C0C0] [color=#008000]"[color=#C0C0C0] [color=#000000]#parentType[color=#C0C0C0] [color=#008000]"[color=#C0C0C0] [color=#008000][size=2]typedefs"), but am still curious about the answer to the original problem (compile-time template arg name).

If I was using 4.7, how would that be done?

Something like this?

template <class Arg>
constexpr const char *operator"" TypeName( const char * /* Ignore */)
{
return typeid(Arg).name();
}


"Name of type is: " ## "dummyText"TypeName



Unfortunately this won't work...


The declaration of a literal operator template shall have an empty parameterdeclaration-clause, and its template-parameter-list shall have a single template parameter that is a non-type template parameter pack with element type char.
[/quote]
http://www.open-std..../2008/n2765.pdf

Even if there was some way of getting the type information into the user defined literal I just tried static_assert with a user defined literal in gcc 4.7 and the error message is empty, which is a shame.

Proabably the best (and most portable) solution is to try and word your error message appropriately.


gcc's __PRETTY_FUNCTION__ isn't a macro, it's a static const local variable.


That makes sense, since there's no way for the preprocessor to know what the function name will be.

This topic is closed to new replies.

Advertisement