developer tip

C ++ 11 지원을 어떻게 확인합니까?

optionbox 2020. 8. 21. 07:34
반응형

C ++ 11 지원을 어떻게 확인합니까?


컴파일러가 C ++ 11의 특정 기능을 지원하는 경우 컴파일 타임에 감지하는 방법이 있습니까? 예를 들면 다음과 같습니다.

#ifndef VARIADIC_TEMPLATES_SUPPORTED

#error "Your compiler doesn't support variadic templates.  :("

#else

template <typename... DatatypeList>
class Tuple
{
    // ...
}

#endif

Boost.Config 에는 특정 C ++ 11 기능에 대한 지원을 테스트하는 데 사용할 수 있는 많은 매크로 가 있습니다.


라는 상수가 __cplusplusC ++ 컴파일러는 C의 버전으로 설정해야이 ++ 표준은 지원 이를 참조

#if __cplusplus <= 199711L
  #error This library needs at least a C++11 compliant compiler
#endif

Visual Studio 2010 SP1에서는 199711L로 설정되어 있지만 모든 C ++ 11 변경 사항이있는 표준 C ++ 라이브러리에 비해 (일부) 컴파일러 수준 지원 만있는 경우 공급 업체가 이미이를 늘릴만큼 과감한 것인지는 모르겠습니다. .

따라서 다른 답변에서 언급 된 Boost의 정의는 예를 들어 C ++ 11 스레드 및 표준의 다른 특정 부분에 대한 지원이 있는지 알아내는 유일한 방법입니다.


C ++ 11 표준 (§iso.16.8)에 명시된대로 :

__cplusplus 라는 이름 C ++ 번역 단위를 컴파일 할 때 201103L로 정의됩니다 .

해당 매크로의 값을 사용하여 컴파일러가 C ++ 11과 호환되는지 여부를 확인할 수 있습니다.

이제 컴파일러가 C ++ 11 기능의 하위 집합을 지원하는지 확인하는 표준 방법을 찾고 있다면 표준 이식 방법이 없다고 생각합니다. 자세한 정보를 얻으려면 컴파일러 문서 또는 표준 라이브러리 헤더 파일을 확인할 수 있습니다.


나는 이것이 매우 오래된 질문이라는 것을 알고 있지만이 질문은 자주 볼 수 있으며 답변은 약간 구식입니다.

C ++ 14 표준을 사용하는 최신 컴파일러에는 C ++ 11 기능을 포함하여 기능을 확인하는 표준 방법이 있습니다. 포괄적 인 페이지는 https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations에 있습니다.

요약하면 각 기능에는에서 확인할 수있는 정의 된 표준 매크로가 있습니다 #ifdef. 예를 들어 사용자 정의 리터럴을 확인하려면 다음을 사용할 수 있습니다.

#ifdef __cpp_user_defined_literals

확인 지원 C ++ 14 및 기타. GCC 5.2.1에서 테스트.

#include <iostream>

int main(){
        #if __cplusplus==201402L
        std::cout << "C++14" << std::endl;
        #elif __cplusplus==201103L
        std::cout << "C++11" << std::endl;
        #else
        std::cout << "C++" << std::endl;
        #endif

        return 0;
}

특정 컴파일러가 어떤 C ++ 11 기능을 지원하는지 확인하기 위해 작은 테스트 스위트를 작성했습니다. 그러나 이것은 물론 '사전 컴파일 타임'검사입니다.

https://github.com/sloede/cxx11tests


이것을 사용할 수 있습니다 :

#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
    cout << "C++11 is supported";
#else
    cout << "C++11 is not supported";
#endif

For C++11, most compilers except Visual Studio set the __cplusplus macro at 201103L, but any version of Visual Studio sets it at 199711L which is the value used for other compilers for before C++11. This code compares the _cplusplus macro with 201103L for all compilers except Visual Studio, and if the compiler is Visual Studio, it checks if the version of Visual Studio is later than 2015, the first version of Visual Studio which completely supports C++11 (for Visual Studio 2015, the _MSC_VER macro has the value 1900, see this answer).


If you do not want to use Boost.Config and need to test for compilers that support C++11 then checking the value of the constant __cplusplus will do. However, a compiler might support most of the popular features of the C++11 standard yet it does not support the entire specifications. If you want to enable support for specific Visual Studio compilers which are not yet 100% compliant to C++11 specifications then use the following code snippet which allows compiling in Visual Studio 2013:

#if defined(_MSC_VER)
#   if _MSC_VER < 1800 
#       error This project needs atleast Visual Studio 2013
#   endif
#elif __cplusplus <= 199711L
#   error This project can only be compiled with a compiler that supports C++11
#endif

A complete list of versions of the compiler for Visual Studio is provided at How to Detect if I'm Compiling Code With Visual Studio 2008


In the traditional Linux/Unix world, autoconf is traditionally used to test for the presence of libraries and compiler features and bugs placing them into a config.h that you use in your files as needed.


When your check is for a C++11 library availability (not a language feature), for example the <array> header, you can #if __has_include(<array>).

Sometimes checking #if __cplusplus >= 201103L would tell you that you use C++11 but other settings like the standard library version setting in Xcode may still not have the new libraries available (most of them are available in different names ie <tr1/array>)

참고URL : https://stackoverflow.com/questions/5047971/how-do-i-check-for-c11-support

반응형