C++ compile-time template arg name?

Started by
13 comments, last by _moagstar_ 11 years, 10 months ago
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()?
Advertisement
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Maybe something along the lines of this hack can generate a constexpr for you?
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.

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. sad.png
I'll just have to word the message better without specifying the argument type.

This isn't too bad:
[color=#008000]main.cpp:121: error: static assertion failed: "TestB cannot be constructed from cPoint-derived classes or strong cPoint typedefs"
If you only use GCC, there seems a GCC specified solution,
http://stackoverflow...plate-type-in-c
See __PRETTY_FUNCTION__

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.

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.

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....
if you want to find out type of memory (providing memory address)...... you can't :)
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.

This topic is closed to new replies.

Advertisement