In a job interview, I was asked to write a metafunction that determined whether a type was a pointer. This is what I presented:
template
struc
There are different approaches, a common one trying to typedef an invalid type:
#define static_assert(condition) \
typedef char assert ## __LINE__ [((condition)?1:-1)]
This can be used in mostly any context and will trip the compiler if the condition is false, since it would try to typedef an invalid type (array of negative number of elements). It can be used in different contexts:
// namespace level:
static_assert(sizeof(int)==4);
struct type {
// class level:
static_assert(sizeof(int)==4);
void f() {
// function level
static_assert(sizeof(int)==4);
}
};