c++ - 如何断言应该使用 C++11 来编译我的程序?

标签 c++ c++11

我想在我的程序中使用一些 C++11 功能。将来我可能不得不与其他人分享我的源代码。 如何在代码中断言应该使用 C++11 来编译我的程序? 较旧的编译器可能会抛出错误,但我希望清楚地告知用户 C++ 11 是必需的。

如果重要的话,我正在使用以下 C++11 功能:

  • 指定存储大小的枚举
  • 标准共享指针

谢谢

最佳答案

您可以检查 __cplusplus 宏的值是否为 201103L 或更大:

#if __cplusplus < 201103L
#error This code requires C++11
#endif

C++11 16.8 预定义的宏名称:

The following macro names shall be defined by the implementation:

__cplusplus

The name __cplusplus is defined to the value 201103L when compiling a C++ translation unit. (155)

(155) It is intended that future versions of this standard will replace the value of this macro with a greater value. Non-conforming compilers should use a value with at most five decimal digits.

关于c++ - 如何断言应该使用 C++11 来编译我的程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15997869/

相关文章:

c++ - 在开关盒中返回 bool 值

c++ - 如何在 C++ (gcc) 中获得完全限定的函数名称,_不包括_返回类型?

c++ - 由于 vector.push_back() 导致执行过早结束 [C++]

C++ 如何将 emplace_back 用于用户定义的结构

c++ - 为什么不能捕获我的 C++ lambda 函数?

c++ - 为什么 gcc 使用我的自定义迭代器优化掉这个 C++11 foreach 循环?

c++ - 使用 asio 的线程安全协程

c++ - Qt - 这些代码行是什么意思

c++ - 为什么用 std::forward 禁用模板参数推导?

C++ 避免调用抽象基类的构造函数