이름공간
변수
행위

조건부 포함

cppreference.com
 
 
C++ 언어
General topics
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Initialization
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 
 

선행처리기는 여러 소스 파일들간의 조건부 결합을 지원합니다. 관련 구문은 아래와 같습니다.

목차

[편집] 구문

#if expression
#ifdef identifier
#ifndef identifier
#elif expression
#else
#endif

[편집] 설명

조건부 선행처리는 #if, #ifdef 또는 #ifndef 명령어로 시작되고, 이후로 선택적으로 여러 개의 #elif 명령어와 적어도 한 개의 #else 명령어를 작성한 후 #endif 명령어로 종료됩니다. Any inner conditional preprocessing blocks are processed separately.

Each of #if, #elif, #else, #ifdef and #ifndef directives control code block until first #elif, #else, #endif directive not belonging to any inner conditional preprocessing blocks.

#if, #ifdef and #ifndef directives test the specified condition (see below) and if it evaluates to true, compiles the controlled code block. In that case subsequent #else and #elif directives are ignored. Otherwise, if the specified condition evaluates false, the controlled code block is skipped and the subsequent #else or #elif directive (if any) is processed. In the former case, the code block controlled by the #else directive is unconditionally compiled. In the latter case, the #elif directive acts as if it was #if directive: checks for condition, compiles or skips the controlled code block based on the result, and in the latter case processes subsequent #elif and #else directives. The conditional preprocessing block is terminated by #endif directive.

[편집] Condition evaluation

[편집] #if, #elif

The expression is a constant expression.

If the expression contains unary operators in form defined identifier or defined (identifier), it is evaluated first. The result is 1 if the identifier was defined as a macro name or the identifier is __has_include (since C++17), otherwise the result is 0.

After macro expansion and evaluation of the defined operator, any identifier which is not a boolean literal are replaced with the number 0.

If the expression evaluates to nonzero value, the controlled code block is included and skipped otherwise.

Note: #if cond1 ... #elif cond2 is different from #if cond1 ... #else followed by #if cond3 because if cond1 is true, the second #if is skipped and cond3 does not need to be well-formed, while #elif's cond2 must be a valid expression.

(until C++14)

[편집] #ifdef, #ifndef

식별자가 이미 매크로로 정의되었는지 확인합니다.

#ifdef identifier#if defined identifier와 같다..

#ifndef identifier#if !defined identifier와 같다..

[편집] 예시

#define ABCD 2
#include <iostream>
 
int main()
{
 
#ifdef ABCD
    std::cout << "1: yes\n";
#else
    std::cout << "1: no\n";
#endif
 
#ifndef ABCD
    std::cout << "2: no1\n";
#elif ABCD == 2
    std::cout << "2: yes\n";
#else
    std::cout << "2: no2\n";
#endif
 
#if !defined(DCBA) && (ABCD < 2*4-3)
    std::cout << "3: yes\n";
#endif
}

Output:

1: yes
2: yes
3: yes

[편집] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
CWG 1955 C++14 failed #elif's expression was required to be valid failed elif is skipped

[편집] See also

C documentation for Conditional inclusion