Inside templates, you get what Daniel Frey's has explained. Outside templates, this isn't possible with static_assert
alone but can be accomplished with the help of a macro and the stringification operator #
:
#define VERIFY_POD(T) \
static_assert(std::is_pod<T>::value, #T " must be a pod-type" );
For the type struct non_pod { virtual ~non_pod() {} };
with gcc 4.8.1, VERIFY_POD(non_pod)
gives
main.cpp:4:2: error: static assertion failed: non_pod must be a pod-type
static_assert(std::is_pod<T>::value, #T " must be a pod-type" );
^
main.cpp:15:2: note: in expansion of macro 'VERIFY_POD'
VERIFY_POD(non_pod);
If you're like me and don't want to see the tokens #T " must be a pod-type"
in the error message, then you can add an extra line to the macro definition:
#define VERIFY_POD(T) \
static_assert(std::is_pod<T>::value, \
#T "must be a pod-type" );
with this, the previous example yields:
main.cpp: In function 'int main()':
main.cpp:4:2: error: static assertion failed: non_pod must be a pod-type
static_assert(std::is_pod<T>::value, \
^
main.cpp:14:2: note: in expansion of macro 'VERIFY_POD'
VERIFY_POD(non_pod);
^
Of course, the exact look of the error message depends on the compiler. With clang 3.4 we get
main.cpp:14:5: error: static_assert failed "non_pod must be a pod-type"
VERIFY_POD(non_pod);
^~~~~~~~~~~~~~~~~~~
main.cpp:3:23: note: expanded from macro 'VERIFY_POD'
#define VERIFY_POD(T) \
^
1 error generated.