189

I want to use C++17 features.

How can I switch compiling from C++14 to C++17 in Microsoft Visual Studio?

Or is it not available in release versions of VS?

Aaron Liu
  • 361
  • 2
  • 13
Tudvari
  • 2,715
  • 2
  • 14
  • 33
  • 4
    Which C++17 features? Which version of Visual Studio? If you're asking about C++17 why did you tag your question `c++11` and `c++14`? – ildjarn Dec 23 '16 at 23:35
  • 9
    c++17 wasn't available, and I thought that those who are interested in specific versions of C++ knows better how to switch it. For example I want std::vector.emplace_back() to give back a reference to the newly created element. I know that +1 line, and I get that reference, But I would like to know the answer to my question anyway. VS Community 2015 (14.0.25431.01 Update 3) – Tudvari Dec 23 '16 at 23:41
  • You should probably check here: https://blogs.msdn.microsoft.com/visualstudio/ – Mikel F Dec 23 '16 at 23:41
  • 3
    You can't enable specific language dialects in VS, you get whatever they've implemented. You might have pretty good luck with standard library features voted into C++17 if you're using VS21015, but not so much with language features. – Praetorian Dec 23 '16 at 23:51
  • Check this out: https://www.visualstudio.com/vs/visual-studio-2017-rc/ . VS 2017 rc should probably support new c++17 features – Semyon Burov Dec 23 '16 at 23:58
  • 1
    @preat MSVC 2017 has a "latest" setting, but that is not released yet. – Yakk - Adam Nevraumont Dec 24 '16 at 01:44

6 Answers6

265

There's now a drop down (at least since VS 2017.3.5) where you can specifically select C++17. The available options are (under project > Properties > C/C++ > Language > C++ Language Standard)

  • ISO C++14 Standard. msvc command line option: /std:c++14
  • ISO C++17 Standard. msvc command line option: /std:c++17

Visual Studio 2022 (MSVC C++20 and the /std:c++20 Switch - C++ Team Blog):

  • ISO C++20 Standard. msvc command line option: /std:c++20

Any Visual Studio:

  • The latest draft standard. msvc command line option: /std:c++latest
li ki
  • 342
  • 3
  • 11
Johan Lundberg
  • 26,184
  • 12
  • 71
  • 97
  • 4
    The syntax `-std:c++14`, `-std:c++17` and `-std:c++latest` also works, e.g. when you prefer to manually enter compiler options (tested with VS2017). – Roi Danton Jul 18 '18 at 07:39
  • 1
    Is that not clear from the answer already, or do you specifically mean `-` instead of the `/std:` switches I list? – Johan Lundberg Jul 18 '18 at 11:59
  • 3
    I see the benefit when coming from `gcc` or `clang`. Then the `-` is less intrusive (however the syntax still differs slightly (`:` instead of `=`)). – Roi Danton Jul 18 '18 at 12:06
  • In VS 2019, to get to Project Properties, go to "Solution Explorer", right-clicked on the project title and select Properties. Then follow directions of this post. – Babar-Baig Dec 04 '19 at 08:36
  • @BabarBaig, that works but the way I described it works just as before. You need to "be" in the relevant project, for example by opening a source code file or selecting the it in the solution explorer. – Johan Lundberg Dec 04 '19 at 09:55
  • `ISO C++20` what version of the Visual Studio? – Andrei Krasutski Dec 26 '19 at 13:29
  • 1
    @AndreiKrasutski. There is no ISO C++20 yet but some support in /std:c++latest. The C++20 standard is expected next year. I Edited in a comment in my answer. – Johan Lundberg Dec 26 '19 at 21:46
  • 3
    What C++ standard is used if there is nothing selected in Visual Studio 2017 > Project Properties > C / C++ > Language > C++ Standard dropdown? I have 14, 17 and latest in the dropdown, but none is selected, so how do I find what c++ standard is used in a project in that case? – pixel Sep 17 '20 at 16:51
  • @pixel It looks as though the default is still C++14 in VS2019: https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/build/reference/std-specify-language-standard-version.md – Antony Hatchkins Apr 25 '21 at 06:12
39

MSBuild (Visual Studio project/solution *.vcproj/*.sln):

Add to Additional options in Project Settings: /std:c++latest to enable latest features - currently C++17 as of VS2017, VS2015 Update 3.

https://blogs.msdn.microsoft.com/vcblog/2016/06/07/standards-version-switches-in-the-compiler/

/permissive- will disable non-standard C++ extensions and will enable standard conformance in VS2017.

https://blogs.msdn.microsoft.com/vcblog/2016/11/16/permissive-switch/

EDIT (Oct 2018): The latest VS2017 features are documented here:

https://learn.microsoft.com/en-gb/cpp/build/reference/std-specify-language-standard-version

VS2017 supports: /std:[c++14|c++17|c++latest] now. These flags can be set via the project's property pages:

To set this compiler option in the Visual Studio development environment

  1. Open the project's Property Pages dialog box. For details, see Working with Project Properties.
  2. Select Configuration Properties, C/C++, Language.
  3. In C++ Language Standard, choose the language standard to support from the dropdown control, then choose OK or Apply to save your changes.

CMake:

Visual Studio 2017 (15.7+) supports CMake projects. CMake makes it possible to enable modern C++ features in various ways. The most basic option is to enable a modern C++ standard by setting a target's property in CMakeLists.txt:

add_library (${PROJECT_NAME})
set_property (TARGET ${PROJECT_NAME}
  PROPERTY
    # Enable C++17 standard compliance
    CXX_STANDARD 17
)

In the case of an interface library:

add_library (${PROJECT_NAME} INTERFACE)
target_compile_features (${PROJECT_NAME}
  INTERFACE
    # Enable C++17 standard compliance
    cxx_std_17
)
Zingam
  • 4,498
  • 6
  • 28
  • 48
11

Visual studio 2019 version:

The drop down menu was moved to:

  • Right click on project (not solution)
  • Properties (or Alt + Enter)
  • From the left menu select Configuration Properties
  • General
  • In the middle there is an option called "C++ Language Standard"
  • Next to it is the drop down menu
  • Here you can select Default, ISO C++ 14, 17 or latest
sanitizedUser
  • 1,723
  • 3
  • 18
  • 33
  • 3
    Remember to do this for 'All Configurations' and 'All Platforms'. That's from someone who's spent an 2 hour figuring out why it still doesn't work! But thanks for the answer. – Persixty Nov 07 '20 at 16:40
  • 1
    In the vs2019 that Unreal setup, there is no Configuration Settings -> General -> C++ Language Standard. Can it be added? There is an nmake -> Additional Options that is set to /std:c++14 ... but changing it to c++17 doesn't seem to work. – zBeeble Jan 25 '22 at 20:20
10

Visual Studio 2015 Update 3 does not support the C++17 feature you are looking for (emplace_back() returning a reference).

Support For C++11/14/17 Features (Modern C++)

C++11/14/17 Features In VS 2015 RTM

VS 2015 Update 2’s STL is C++17-so-far Feature Complete

Visual Studio 2015 Update 3

STL Fixes In VS 2015 Update 3

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
6

If bringing existing Visual Studio 2015 solution into Visual Studio 2017 and you want to build it with c++17 native compiler, you should first Retarget the solution/projects to v141 , THEN the dropdown will appear as described above ( Configuration Properties -> C/C++ -> Language -> Language Standard)

WilliamClements
  • 350
  • 3
  • 8
-1

VS Code 2020 version

In tasks.json file, (after you build and debug with the g++-9)

Add -std=c++2a for 2020 features (c++1z for 2017 features). Add -fconcepts to use concept keyword

"args": [
   "-std=c++2a",
   "-fconcepts",
   "-g",
   "${file}",
   "-o",
   "${fileDirname}/${fileBasenameNoExtension}"
],

now compile and you can use the 2020 features.

Ahmet
  • 7,527
  • 3
  • 23
  • 47
  • 1
    There is no Visual Studios 2020, this is for V.S. Code – JΛYDΞV Aug 31 '21 at 18:13
  • But the question is not asking "only-for" VS Studio. If you re-read the question, at the end of the line "it's not avaiable in release versions of VS?". The question is flexible, no need to be strict. – Ahmet Sep 06 '21 at 12:26
  • 1
    This question is not that flexible, and if it were, it would need to be closed. It wouldn't be focused enough. VS Code is not a different version of Visual studio. One is an Integrated Development Environment, and one is an Editor. One pulls up the code that you write, the other is embedded into the file system of the project it is working on. One is funded by Microsoft, one is developed by Microsoft. One is portable to Linux, one is not. A configuration for one, will not work for the other. You might as well answer how to compile C++17 in Notepad. – JΛYDΞV Sep 06 '21 at 21:58
  • @Ahmet There's no such thing as VS Studio anyway. VS is quite clear and targeted. – ashrasmun Aug 21 '23 at 13:55
  • @ashrasmun Searching before making a comment is always better. Now VSCode, previously Visual Studio (https://www.microsoft.com/en-US/download/details.aspx?id=40784) – Ahmet Aug 21 '23 at 14:05
  • @Ahmet Visual Studio and Visual Studio Code are different tools. Their names have never changed. Stop acting snarky. – ashrasmun Aug 22 '23 at 13:05