Namespaces
Variants

Comments

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

주석은 일종의 코드 내 문서화 역할을 합니다. 프로그램에 삽입되면 컴파일러에 의해 효과적으로 무시됩니다; 주석은 소스 코드를 읽는 사람들을 위한 메모로만 사용되도록 의도되었습니다. 비록 구체적인 문서화가 C++ 표준의 일부는 아니지만, 여러 유틸리티들이 다양한 문서화 형식의 주석을 파싱합니다.

목차

구문

/* 주석 */ (1)
// 주석 (2)
1) 흔히 "C 스타일" 또는 "다중 라인" 주석으로 알려져 있습니다.
2) 흔히 "C++ 스타일" 또는 "한 줄" 주석으로 알려져 있습니다.

모든 주석은 프로그램에서 번역 단계 3 에서 제거되며, 각 주석을 단일 공백 문자로 대체합니다.

C 스타일

C 스타일 주석은 일반적으로 큰 텍스트 블록을 주석 처리하는 데 사용되지만, 단일 라인을 주석 처리하는 데에도 사용할 수 있습니다. C 스타일 주석을 삽입하려면 텍스트를 /* */ 로 감싸면 됩니다. 이렇게 하면 주석 내용이 컴파일러에 의해 무시됩니다. 비록 C++ 표준의 일부는 아니지만, /** */ 는 종종 문서 블록을 나타내는 데 사용됩니다. 두 번째 별표는 단순히 주석의 일부로 처리되기 때문에 이것은 문법적으로 유효합니다. C 스타일 주석은 중첩될 수 없습니다.

C++ 스타일

C++-style 주석은 일반적으로 한 줄을 주석 처리하는 데 사용되지만, 여러 개의 C++-style 주석을 함께 배치하여 다중 줄 주석을 형성할 수 있습니다. C++-style 주석은 컴파일러에게 // 과 새 줄 사이의 모든 내용을 무시하도록 지시합니다.

참고 사항

주석은 제거되기 전처리기 단계 이전에, 매크로는 주석을 형성하는 데 사용될 수 없으며 종료되지 않은 C-스타일 주석은 #include된 파일에서 유출되지 않습니다.

주석 처리 외에, 소스 코드 제외에 사용되는 다른 메커니즘들은

#if 0
    std::cout << "이 코드는 실행되지도 컴파일되지도 않습니다\n";
#endif

그리고

if (false)
{
    std::cout << "이 코드는 실행되지 않습니다\n";
}

예제

#include <iostream>
/* C-style comments can contain
multiple lines */
/* or just one */
/**************
 *  you can insert any *, but
 *  you can't make comments nested
 */
// C++-style comments can comment one line
// or, they can
// be strung together
int main()
{
    // comments are removed before preprocessing,
    // so ABC is "1", not "1//2134", and "1 hello world"
    // will be printed
#define ABC 1//2134
    std::cout << ABC << " hello world\n";
    // The below code won't be run
    // return 1;
    // The below code will be run
    return 0;
}

출력:

1 hello world

참고 항목

C 문서 에서 comment 참조