F
Frederick Gotham
Is there any way to force compile failure if a particular compile-time
constant doesn't evaluate to true?
Consider a template which was intended to only work with unsigned
integers (whether they be char, short, int, long):
#include <limits>
template<class T>
void Func()
{
CompileErrorIfTrue( std::numeric_limits<T>::is_signed );
/* Rest of function */
}
I suppose we could do something like:
template<bool condition>
void CompileErrorIfTrue();
/* Definition missing in order to trigger compile error */
template<>
void CompileErrorIfTrue<false>() {}
#include <limits>
template<class T>
void Func()
{
CompileErrorIfTrue<std::numeric_limits<T>::is_signed>();
/* Rest of function */
}
int main()
{
Func<int>();
}
constant doesn't evaluate to true?
Consider a template which was intended to only work with unsigned
integers (whether they be char, short, int, long):
#include <limits>
template<class T>
void Func()
{
CompileErrorIfTrue( std::numeric_limits<T>::is_signed );
/* Rest of function */
}
I suppose we could do something like:
template<bool condition>
void CompileErrorIfTrue();
/* Definition missing in order to trigger compile error */
template<>
void CompileErrorIfTrue<false>() {}
#include <limits>
template<class T>
void Func()
{
CompileErrorIfTrue<std::numeric_limits<T>::is_signed>();
/* Rest of function */
}
int main()
{
Func<int>();
}