Namespaces
Variants

Diagnostic directives

From ja.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

指定されたエラーメッセージを表示し、プログラムを不適格にする 、または指定された警告メッセージを表示し、プログラムの有効性に影響を与えない (C++23以降) 。

目次

構文

#error 診断メッセージ (1)
#warning 診断メッセージ (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 エラーディレクティブ [cpp.error]
  • C++20標準 (ISO/IEC 14882:2020):
  • 15.8 エラーディレクティブ [cpp.error]
  • C++17 標準 (ISO/IEC 14882:2017):
  • 19.5 エラーディレクティブ [cpp.error]
  • C++14規格 (ISO/IEC 14882:2014):
  • 16.5 エラーディレクティブ [cpp.error]
  • C++11規格 (ISO/IEC 14882:2011):
  • 16.5 エラーディレクティブ [cpp.error]
  • C++03規格 (ISO/IEC 14882:2003):
  • 16.5 エラーディレクティブ [cpp.error]
  • C++98標準 (ISO/IEC 14882:1998):
  • 16.5 エラーディレクティブ [cpp.error]

関連項目

C ドキュメント for 診断ディレクティブ