Diagnostic directives
From cppreference.com
 
 
 < cpp | preprocessor 
 
 
 C++ 
 Feature test macros (C++20)
 Concepts library (C++20)
 Metaprogramming library (C++11)
 Ranges library (C++20)
 Filesystem library (C++17)
 Concurrency support library (C++11)
 Execution control library (C++26)
C++ language 
 
  
  
 General topics
 Conditional execution statements
 Iteration statements (loops)
 Jump statements
 Dynamic exception specifications (until C++17*)
noexcept specifier (C++11) Exceptions
 Namespaces
 Types
 Specifiers
 User-defined (C++11)
 Utilities
 Attributes (C++11)
 Types
 Type alias declaration (C++11)
 Casts
 Memory allocation
 Class-specific function properties
 Special member functions
 Miscellaneous
Preprocessor 
 
 
 
 
 
(C++23)(C++23)
(C++17)
#error#warning
(C++23)
(C++11)
(C++26)
Shows the given error message and renders the program ill-formed, or shows the given warning message without affecting the validity of the program(since C++23).
[edit] Syntax
#error diagnostic-message
 (1)
 
#warning diagnostic-message
 (2)
 (since C++23)
[edit] Explanation
1) After encountering the #error directive, an implementation displays the message diagnostic-message and renders the program ill-formed (the compilation stops).
2) Same as (1), except the validity of the program is not affected and the compilation continues.
diagnostic-message can consist of several words not necessarily in quotes.
[edit] Notes
Before its standardization in C++23, #warning has been provided by many compilers in all modes as a conforming extension.
[edit] Example
Run this code
#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\n"; }
Possible output:
The implementation used is hosted
[edit] References
- C++23 standard (ISO/IEC 14882:2024):
- 15.8 Error directive [cpp.error]
 
- C++20 standard (ISO/IEC 14882:2020):
- 15.8 Error directive [cpp.error]
 
- C++17 standard (ISO/IEC 14882:2017):
- 19.5 Error directive [cpp.error]
 
- C++14 standard (ISO/IEC 14882:2014):
- 16.5 Error directive [cpp.error]
 
- C++11 standard (ISO/IEC 14882:2011):
- 16.5 Error directive [cpp.error]
 
- C++03 standard (ISO/IEC 14882:2003):
- 16.5 Error directive [cpp.error]
 
- C++98 standard (ISO/IEC 14882:1998):
- 16.5 Error directive [cpp.error]
 
[edit] See also
C documentation  for Diagnostic directives