C++ compile-time template arg name?

This topic is 2065 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

How can I do the following?

template<typename Type> Func(Type type) { static_assert( ...condition... , "The type is: " ## typeid(Arg).name() ); } int myInt; Func(myInt); //Should give a compile time error of, "The type is: int" 

I'm using MinGW/GCC, with C++11 features enabled. typeid() is run-time, but static_assert() is compile time. I'm wondering if there's a compile-time way to get the type name of a template argument. (In this case, not 'Type', 'type', or 'myInt', but should give the actual name of type used: 'int'.

Anyone know if this is possible? With all the metaprogramming additions C++11 added, I'm hoping there's something like that in the new library additions somewhere, I just can't find it.

Also, how can I concatenate two strings or string literals at compile-time to pass to static_assert()? Edited by Servant of the Lord

Share on other sites
Well, you can get most of the way there with this:

static_assert(std::is_same<Type, int>::value, "Type must be int");

I'm not entirely sure what the motivation for wanting to print the type name in the assert is though? There might be a different want to accomplish whatever you're trying to do.

Share on other sites
Maybe something along the lines of this hack can generate a constexpr for you?

Share on other sites
Unfortunately I don't think you can do anything fancy in a static_assert.

It all comes down to the compiler, clang for instance will usually print out enough informations on a static_assert failure to know which template is being instanced and with which types.

Edit: I just checked the standard, the second parameter of static_assert is a string-literal. So it can only ever just be a string, the only way you could possibly concatenate stuff in it would be by using the preprocessor, so you don't have a lot of possibilities. Edited by Zlodo

Share on other sites

Well, you can get most of the way there with this:

static_assert(std::is_same<Type, int>::value, "Type must be int");

I'm not entirely sure what the motivation for wanting to print the type name in the assert is though? There might be a different want to accomplish whatever you're trying to do.

I have a class constructor function like this:
 template<typename Arg> Derived(Arg &arg) : Parent(arg) { static_assert((std::is_base_of<parentType, Arg>::value == false), "" #Derived " cannot be constructed from " TO_STRING(typeid(Arg).name())); } 
Outputs: "[color=#006400]static assertion failed: "Derived cannot be constructed from typeid(Arg).name()" "
(Macroes being processed before templates, thus converting "typeid(Arg).name()" to a string literal before it gets resolved... even if typeid() was compile-time, which it is not).

It works fine and does what I want, warning at compile-time that you can't pass Derived() an argument that's derived from 'Parent'. Multiple classes are possibly derived from Parent and I don't want Parent, or other Parent-derived classes, being passed in to the constructor. Other classes and variables are allowed in, but the only Parent-derived class permitted to be passed in is 'Derived' itself (i.e. through the copy-constructor).

I was just hoping to get a cleaner error message, like: "Derived cannot be constructed from OtherDerivedType".
I could just say, "Derived cannot be constructed from any Parent-derived class" but that requires the programmer to remember that "OtherDerivedType" is derived from Parent, when it may not be obvious.

Template meta-programming is something new to me* - this is the first time I've needed to use it to do what I want. I'm adding strong typedefs to C++ through templates and a macro - they are now almost fully working, and it's not that messy either (and works how one assumes strong typedefs would work). I'll make a journal post later describing how it works, so others can point out any mistakes in the code or adopt the code for themselves.

*[size=2]Luckily this hardly qualifies as 'meta-programming' (if at all), so it's a fairly easy introduction. It's actually not really meta-programming at all, more of just compile-time logic.

So it's not a big deal, since the code itself works. I am just wanting a better (more descriptive) error message if Derived is constructed from Parent or another Parent-derived class. I'm

It all comes down to the compiler, clang for instance will usually print out enough informations on a static_assert failure to know which template is being instanced and with which types.

Yea, GCC does too, so it's easy to locate the mistake, I just wanted a better error message to explain why it's a mistake.

Edit: I just checked the standard, the second parameter of static_assert is a string-literal. So it can only ever just be a string, the only way you could possibly concatenate stuff in it would be by using the preprocessor, so you don't have a lot of possibilities.
[/quote]
Well, that kills that then.
I'll just have to word the message better without specifying the argument type.

[color=#008000]main.cpp:121: error: static assertion failed: "TestB cannot be constructed from cPoint-derived classes or strong cPoint typedefs" Edited by Servant of the Lord

Share on other sites
If you only use GCC, there seems a GCC specified solution,
http://stackoverflow...plate-type-in-c
See __PRETTY_FUNCTION__ Edited by wqking

Share on other sites
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.

Share on other sites

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.

first thing to remember, template does not know its most derived type!

second: the template is a (any type on the road) type you are sure it derives from and performs the routione on that type, not minding next....

third: template is handsome in few few cases, be aware of tamplates unless you are defining collections of objects, or some abstraction of objects set....

Share on other sites
if you want to find out type of memory (providing memory address)...... you can't

Share on other sites
how you behave to memory lies totaly on you.... it is in your matter you know what type it is. Let me write something like this

[source]
SuperBoy do (object boy)
{
return SuperBoy=(SuperBoy)boy;
}
[/source]

you retyped object to SuperBoy......
maybe will work, up to your matter.

so.
templates can abstract handling any objects,

templates are good, has saved my day through tham some times, but when I did, I was allways coding collections of uncertain types that could provide certain routine on the collection.

Share on other sites

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.

Share on other sites
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.

Share on other sites

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.

Share on other sites

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).

Share on other sites

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. Edited by _moagstar_