考虑以下函数:
template<int N> f()
{
if(N==5)
{
std::cerr << "N=5 is not supported!\n";
abort();
}
do_real_work(N);
}
如何将运行时错误消息转换为编译时错误?如果我使用 C++11,我想我可以使用 static_assert
,但是 C++03 呢?
最佳答案
您可以使用BOOST.StaticAssert
:
#include <boost/static_assert.hpp>
BOOST_STATIC_ASSERT_MSG(N == 5, "N=5 is not supported!\n")
关于c++ - 为某些模板参数发出编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19466649/