诊断指令
来自cppreference.com
< cpp | preprocessor
C++ 语言
一般主题 | |||||||||||
流程控制 | |||||||||||
条件执行语句 | |||||||||||
重复语句(循环) | |||||||||||
跳转语句 | |||||||||||
函数 | |||||||||||
函数声明 | |||||||||||
Lambda 函数声明 | |||||||||||
inline 说明符
|
|||||||||||
动态异常说明 (C++20 前) | |||||||||||
noexcept
说明符 (C++11) |
|||||||||||
异常 | |||||||||||
命名空间 | |||||||||||
类型 | |||||||||||
说明符 | |||||||||||
|
|||||||||||
存储期说明符 | |||||||||||
初始化 | |||||||||||
表达式 | ||||||||||||||||
替代表示 | ||||||||||||||||
字面量 | ||||||||||||||||
布尔 - 整数 - 浮点 | ||||||||||||||||
字符 - 字符串 - nullptr (C++11) | ||||||||||||||||
用户定义 (C++11) | ||||||||||||||||
工具 | ||||||||||||||||
属性 (C++11) | ||||||||||||||||
类型 | ||||||||||||||||
typedef 声明
|
||||||||||||||||
类型别名声明 (C++11) | ||||||||||||||||
类型转换 | ||||||||||||||||
隐式转换 - 显式转换 | ||||||||||||||||
static_cast - dynamic_cast | ||||||||||||||||
const_cast - reinterpret_cast | ||||||||||||||||
内存分配 | ||||||||||||||||
类 | ||||||||||||||||
特定于类的函数性质 | ||||||||||||||||
|
||||||||||||||||
特殊成员函数 | ||||||||||||||||
模板 | ||||||||||||||||
杂项 | ||||||||||||||||
预处理器
(C++23)(C++23)
|
||||
(C++17)
|
||||
#error#warning
(C++23)
|
||||
(C++11)
|
||||
显示给定的错误消息并使得程序非良构,或给定的警告消息而不影响程序的合法性 (C++23 起)。
语法
#error 诊断消息
|
(1) | ||||||||
#warning 诊断消息
|
(2) | (C++23 起) | |||||||
解释
1) 实现在遇到
#error
指令后,显示消息 诊断消息 ,并令程序非良构(停止编译)。2) 同 (1) ,除了不影响程序的合法性并且编译继续。
注解
在其于 C++23 的标准化前, #warning
已经被许多编译器作为遵从的扩展提供。
示例
运行此代码
#if __STDC_HOSTED__ != 1 # error "Not a hosted implementation" #endif #if __cplusplus >= 202302L # warning "Using #warning as a standard feature" #endif #include <iostream> int main() { std::cout << "The implementation used is hosted"; }
可能的输出:
The implementation used is hosted
引用
- C++20 标准(ISO/IEC 14882:2020):
-
- 15.8 Error directive [cpp.error]
- C++17 标准(ISO/IEC 14882:2017):
-
- 19.5 Error directive [cpp.error]
- C++14 标准(ISO/IEC 14882:2014):
-
- 16.5 Error directive [cpp.error]
- C++11 标准(ISO/IEC 14882:2011):
-
- 16.5 Error directive [cpp.error]
- C++03 标准(ISO/IEC 14882:2003):
-
- 16.5 Error directive [cpp.error]
- C++98 标准(ISO/IEC 14882:1998):
-
- 16.5 Error directive [cpp.error]