• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Servant of the Lord

C++ compile-time template arg name?

14 posts in this topic

How can I do the following?

[CODE]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"
[/CODE]

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
0

Share this post


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

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

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

Share this post


Link to post
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
1

Share this post


Link to post
Share on other sites
[quote name='ApochPiQ' timestamp='1337736173' post='4942395']
Well, you can get most of the way there with this:

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

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.
[/quote]

I have a class constructor function like this:
[CODE]
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()));
}
[/CODE]
Outputs: "[color=#006400][i]static assertion failed: "Derived cannot be constructed from typeid(Arg).name()" [/i][/color]"
(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: "[i]Derived cannot be constructed from OtherDerivedType[/i]".
I could just say, "[i]Derived cannot be constructed from any Parent-derived class[/i]" 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.[/size]

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


[quote name='Zlodo' timestamp='1337768921' post='4942494']
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.[/quote]
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.

[quote]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. [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]
I'll just have to word the message better without specifying the argument type.

This isn't too bad:
[i][color=#008000]main.cpp:121: error: static assertion failed: "TestB cannot be constructed from cPoint-derived classes or strong cPoint typedefs"[/color][/i] Edited by Servant of the Lord
0

Share this post


Link to post
Share on other sites
If you only use GCC, there seems a GCC specified solution,
[url="http://stackoverflow.com/questions/596185/is-it-possible-to-get-a-char-name-from-a-template-type-in-c"]http://stackoverflow...plate-type-in-c[/url]
See __PRETTY_FUNCTION__ Edited by wqking
0

Share this post


Link to post
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.
0

Share this post


Link to post
Share on other sites
[quote name='Servant of the Lord' timestamp='1337879899' post='4942950']
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.
[/quote]

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

Share this post


Link to post
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.
0

Share this post


Link to post
Share on other sites
[quote name='Servant of the Lord' timestamp='1337879899' post='4942950']
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.
[/quote]
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.
0

Share this post


Link to post
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.
1

Share this post


Link to post
Share on other sites
[quote name='_moagstar_' timestamp='1337952745' post='4943229']
It may be possible to parse the PRETTY_FUNCTION macro to get the text you require.
[/quote]
gcc's __PRETTY_FUNCTION__ isn't a macro, it's a [url=http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html]static const local variable[/url].
0

Share this post


Link to post
Share on other sites
[quote name='_moagstar_' timestamp='1337952745' post='4943229']
Which version of gcc are you using? I believe that user defined literals are supported from version 4.7[/quote]
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?
[CODE]template<class Arg>
constexpr const char *operator"" TypeName( const char * /* Ignore */)
{
return typeid(Arg).name();
}


"Name of type is: " ## "dummyText"TypeName<Arg>[/CODE]

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

Share this post


Link to post
Share on other sites
[quote name='Servant of the Lord' timestamp='1337957934' post='4943254']
If I was using 4.7, how would that be done?

Something like this?

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

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

[/quote]

Unfortunately this won't work...

[quote]
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 [b]non-type[/b] template parameter pack with element type char.
[/quote]
[url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2765.pdf"]http://www.open-std..../2008/n2765.pdf[/url]

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.

[quote name='SiCrane' timestamp='1337953060' post='4943230']
gcc's __PRETTY_FUNCTION__ isn't a macro, it's a static const local variable.
[/quote]

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0