allo
allo

Reputation: 4236

How to compile with VS2017 for an older C++ standard?

I am using Visual Studio 2017 and need to create code that is compatible to VS2008 (C++03 or C++98). Is there a switch to restrict MSVC to C++03 features?

I am using CMake and tried to set

set_property(TARGET tgt PROPERTY CXX_STANDARD 98)

But this seems only to make sure, that the compiler supports C++98 or newer.

Any solution, that checks if C++ code uses features that are newer than the features supported by VS2008 will work as well. I just need to make sure, that I do not accidentally use features that are too new.

Upvotes: 1

Views: 517

Answers (1)

Dan M.
Dan M.

Reputation: 4052

MSVC only got the standard switch in one of the updates to VS2015 (Update 3 to be exact) which was more or less C++14 compliant, and as such there are only switches for standards starting with C++14 (plus a few later features that were already implemented at the time of the update). All older features are enabled unconditionally for backwards compatibility (and because of all the work required to retrofit already implemented features for previous standards for virtually no gain).

See this blog post for more information: https://devblogs.microsoft.com/cppblog/standards-version-switches-in-the-compiler/

Also, note that there were a lot of conformance improvements in newer versions of MSVC, so even with the std switches you could write code that wouldn't work or would behave differently on older compiler.

A better solution would be just to use VS2008 toolset from VS2017 visual studio, as explained here: https://devblogs.microsoft.com/cppblog/stuck-on-an-older-toolset-version-move-to-visual-studio-2015-without-upgrading-your-toolset/

That way you'll be certain your code compiles on the older toolset, while using up-to-date IDE.

Upvotes: 2

Related Questions