Jump to content

  • Log In with Google      Sign In   
  • Create Account

C++ compile-time template arg name?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
14 replies to this topic

#1 Servant of the Lord   Crossbones+   -  Reputation: 21026

Like
0Likes
Like

Posted 22 May 2012 - 07:01 PM

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, 22 May 2012 - 07:05 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


Sponsor:

#2 ApochPiQ   Moderators   -  Reputation: 16401

Like
1Likes
Like

Posted 22 May 2012 - 07:22 PM

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.

#3 Hodgman   Moderators   -  Reputation: 31843

Like
1Likes
Like

Posted 22 May 2012 - 07:47 PM

Maybe something along the lines of this hack can generate a constexpr for you?

#4 Zlodo   Members   -  Reputation: 246

Like
1Likes
Like

Posted 23 May 2012 - 04:28 AM

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, 23 May 2012 - 04:35 AM.


#5 Servant of the Lord   Crossbones+   -  Reputation: 21026

Like
0Likes
Like

Posted 23 May 2012 - 11:44 AM

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: "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.

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

Well, that kills that then. Posted Image
I'll just have to word the message better without specifying the argument type.

This isn't too bad:
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, 23 May 2012 - 11:48 AM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#6 wqking   Members   -  Reputation: 756

Like
0Likes
Like

Posted 24 May 2012 - 12:07 AM

If you only use GCC, there seems a GCC specified solution,
http://stackoverflow...plate-type-in-c
See __PRETTY_FUNCTION__

Edited by wqking, 24 May 2012 - 12:09 AM.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#7 Servant of the Lord   Crossbones+   -  Reputation: 21026

Like
0Likes
Like

Posted 24 May 2012 - 11:18 AM

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.
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#8 JohnnyCode   Members   -  Reputation: 294

Like
0Likes
Like

Posted 24 May 2012 - 04:42 PM

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

#9 JohnnyCode   Members   -  Reputation: 294

Like
0Likes
Like

Posted 24 May 2012 - 04:43 PM

if you want to find out type of memory (providing memory address)...... you can't :)

#10 JohnnyCode   Members   -  Reputation: 294

Like
0Likes
Like

Posted 24 May 2012 - 04:49 PM

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


SuperBoy do (object boy)

{

	 return SuperBoy=(SuperBoy)boy;

}



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.

#11 wqking   Members   -  Reputation: 756

Like
0Likes
Like

Posted 24 May 2012 - 07:56 PM

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.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#12 _moagstar_   Members   -  Reputation: 465

Like
1Likes
Like

Posted 25 May 2012 - 07:32 AM

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.

#13 SiCrane   Moderators   -  Reputation: 9670

Like
0Likes
Like

Posted 25 May 2012 - 07:37 AM

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.

#14 Servant of the Lord   Crossbones+   -  Reputation: 21026

Like
0Likes
Like

Posted 25 May 2012 - 08:58 AM

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 ("" #newTypeName " is not implicitely convertible with " #parentType "-derived classes or strong " #parentType " typedefs"), but am still curious about the answer to the original problem (compile-time template arg name).
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#15 _moagstar_   Members   -  Reputation: 465

Like
0Likes
Like

Posted 28 May 2012 - 10:24 AM

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.

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_, 28 May 2012 - 10:26 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS