Namespaces
Variants

Diagnostic directives

From cppreference.net
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications ( until C++17* )
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous

주어진 오류 메시지를 표시하고 프로그램을 ill-formed 상태로 만듭니다 , 또는 주어진 경고 메시지를 표시하면서 프로그램의 유효성에 영향을 주지 않습니다 (C++23부터) .

목차

구문

#error diagnostic-message (1)
#warning diagnostic-message (2) (C++23부터)

설명

1) #error 지시문을 만난 후, 구현체는 메시지 diagnostic-message 를 표시하고 프로그램을 비형식으로 만듭니다(컴파일이 중지됩니다).
2) (1) 과 동일하지만, 프로그램의 유효성에는 영향을 미치지 않으며 컴파일이 계속 진행됩니다.

diagnostic-message 는 따옴표가 필요 없는 여러 단어로 구성될 수 있습니다.

참고 사항

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\n";
}

가능한 출력:

The implementation used is hosted

참고문헌

  • C++23 표준 (ISO/IEC 14882:2024):
  • 15.8 Error 지시자 [cpp.error]
  • C++20 표준(ISO/IEC 14882:2020):
  • 15.8 Error 지시자 [cpp.error]
  • C++17 표준 (ISO/IEC 14882:2017):
  • 19.5 Error 지시자 [cpp.error]
  • C++14 표준(ISO/IEC 14882:2014):
  • 16.5 Error 지시자 [cpp.error]
  • C++11 표준 (ISO/IEC 14882:2011):
  • 16.5 Error directive [cpp.error]
  • C++03 표준 (ISO/IEC 14882:2003):
  • 16.5 Error 지시자 [cpp.error]
  • C++98 표준 (ISO/IEC 14882:1998):
  • 16.5 Error directive [cpp.error]

참고 항목

C 문서 참조: 진단 지시문