CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,851

    Identifying the compiler language

    I'm not talking about compilers, like MSVC or Clang etc - but within Visual Studio I can select C++14 or 17 or 20 etc. But C++14 offered features which got abandoned later (such as std::auto_ptr) and of course the later versions offer new features which weren't in C++14. So is there some way I can identify the language at compile time? Something like...

    Code:
    #if (I_SELECTED_C++17_OR_HIGHER)
         // Build a small code section using the newer features
    #else
         // Carry on building it the old way
    #endif
    }
    Last edited by John E; October 10th, 2024 at 02:04 AM.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,857

    Re: Identifying the compiler language

    At compile time you can ascertain the version of the compiler being used with _MSC_VER or _MSC_FULL_VER See:
    https://learn.microsoft.com/en-us/cp...?view=msvc-170
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,851

    Re: Identifying the compiler language

    Sorry if I've misunderstood that page but it's not the compiler version I need - e.g. VS2013 as opposed to VS2019. What I'm looking for is which language version I've selected - e.g C++14 as opposed to C++17.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,857

    Re: Identifying the compiler language

    There is the __cplusplus macro. See:

    https://learn.microsoft.com/en-us/cp...?view=msvc-170

    Also have a look at vcruntime.h at around lines 275 - 300 for how MS tests for different C++ versions in the standard library.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,851

    Re: Identifying the compiler language

    Thanks... it looks like _MSVC_LANG might be what I need, though its name suggests it mightn't work if I'm using the Clang compiler option I'll try an experiment here...
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  6. #6
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,851

    Re: Identifying the compiler language

    Woohoo! _MSVC_LANG seems to work for both MSVC and Clang. Many thanks, 2kaud

    BTW - CodeGuru seems to have gone back to not sending email notifications when someone replies to a thread. I thought that got fixed months ago
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,415

    Re: Identifying the compiler language

    Quote Originally Posted by John E View Post
    Woohoo! _MSVC_LANG seems to work for both MSVC and Clang. Many thanks, 2kaud

    ... I thought that got fixed months ago
    Yes, that got fixed for a very short period. But then ...

    However, private notifications do work as they do for many decades.
    Victor Nijegorodov

  8. #8
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,857

    Re: Identifying the compiler language

    The list of MS supported macros is at:

    https://learn.microsoft.com/en-us/cp...?view=msvc-170

    However as _MSVC_LANG isn't standard there's no guarantee it'll work with another compiler (excluding clang under VS as that's still really VS). __cplusplus is standard across compilers.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,851

    Re: Identifying the compiler language

    @VictorN - there's something unsettling somehow about a forum for coders where they can't find a programmer to fix their own site's bugs
    "A problem well stated is a problem half solved.” - Charles F. Kettering

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured