본문 바로가기

진리는어디에/C++

[C++20] Feature Test Macro

Feature Test Macro는 C++에서 제공하는 기능을 현재 사용 중인 컴파일러가 지원하는지 그렇지 않은지 여부를 테스트하기 위한 전처리 명령어(매크로)다. 

Attributes

__has_cpp_attribute( attribute-token )
  • 현재 사용하는 컴파일러가 attribute-token에 지정된 attribute를 지원하는지 체크
  • #if, #else, #ifdef, #ifndef를 이용해 컴파일 타임에 지원 여부 체크 가능
int main() { #if __has_cpp_attribute(carries_dependency) ​​​​std::cout << "this compiler supports 'carries_dependency'" << std::endl; #endif #if __has_cpp_attribute(deprecated) ​​​​std::cout << "this compiler supports 'deprecated'" << std::endl; #endif #if __has_cpp_attribute(fallthrough) ​​​​std::cout << "this compiler supports 'fallthrough'" << std::endl; #endif }

[C++20] 어트리뷰트(Attribute) 관련 글 보기

Language Features

#include <iostream> #include <span> #include <chrono> // https://en.cppreference.com/w/cpp/feature_test int main() { ​​​​// 언어 지원 여부 체크 #ifdef __cpp_concepts ​​​​std::cout << "support concepts feature" << std::endl; #endif #ifdef __cpp_modules ​​​​std::cout << "support modules feature" << std::endl; #endif }

Library features

​​​​// 라이브러리 지원 여부 체크 #ifdef __cpp_lib_span // span 헤더 인클루드 필요 ​​​​std::cout << "support span" << std::endl; #endif #ifdef __cpp_lib_chrono // span 헤더 인클루드 필요 ​​​​std::cout << "support chrono" << std::endl; #endif
유익한 글이었다면 공감(❤) 버튼 꾹!! 추가 문의 사항은 댓글로!!