c++ - 为某些模板参数发出编译错误

标签 c++ templates compiler-errors

考虑以下函数:

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/

相关文章:

c++ - 如何正确公开基础模板类型以用作类型

intellij-idea - Intellij IDEA Scala测试-编译错误,但在sbt中可以

c++ - glTexImage2D 多个图像

C++ 常量指针声明

c++ - 使用 vector 数据结构 - 设计和语法问题

javascript - 如何在 Vue js x-template 上的 html 属性中解析字符串?

c++ - 带有 clang 的 MacOSX 中的函数模板特化

c++ - 为什么仅通过使用命名空间std删除就摆脱了错误(对 “”的引用不明确);在C++中?

c# - 重载方法签名 - 编译器将可空类型视为常规值类型的同义词

c++ - 如何循环遍历映射并将值分配给 C++ 中的数组?