I'm looking for a way to invoke a custom compile time error. I'm working with a lot of templates and would like to have the compiler emit a more meaningful message when particular errors occur.
I'm working on a math package with highly flexible matrices, vectors, and other linear algebra constructs. Here's what a matrix declaration may look like.
math::matrix<float, math::dim_static2<4, 4>, math::row_major, math::column_major> mat;
This creates a (statically sized) 4 by 4 matrix of floats with a row major layout in memory using column major vector notation. (btw, I've been working on this due to some problems I had earlier with matricies which I received a lot of help with from this forum. :-) )
The templates I have are designed to work with the meta types above, such as math::row_major. If instead of specifying math::row_major above, a user specifies int, the compiler complains that mat is an instantiation of an abstract type because the at() member function is pure virtual. This is absolutely correct, but the error is confusing and misleading. I'm looking for a way to tell the compiler that if some code is reached, it should output a particular message. Something like this.
template<typename T>
void some_func(const T& arg)
{
#compile_error("only float and double types allowed")
}
void some_func<float>(const float& arg)
{
std::cout << arg;
}
void some_func<double>(const double& arg)
{
std::cout << arg;
}
Here, some_func() can only be instansiated with T = float or T = double. (I realize in this case that overloads would be a much better choice, but I'm just trying to illustrate.)
Is there anything akin to #compile_error as I used it above?
Thanks!