I came across these lines of code:
template< int I > struct arg
{
arg()
{
}
template< class T > arg( T const & /* t */ )
{
// static assert I == is_placeholder<T>::value
typedef char T_must_be_placeholder[ I == is_placeholder<T>::value? 1: -1 ];
}
};
template< class T > struct is_placeholder
{
enum _vt { value = 0 };
};
What could be the reason the struct is_placeholder
is templated while typename T
is not used anywhere inside?
Why T_must_be_placeholder
is defined in such a way so that it can have invalid size -1
. To realise this, I called arg<1>(1)
and it gave error: size of array is negative
as expected. Is it some sort of sanity-check technique? Why doesn't the compiler report this issue if the arg<1>(1)
call is not made?
While
int i = 0;
char a[i == 1 ? 1 : -1]; //No error
If sanity check works for 1st example, then how does it fail for second one?