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