简体   繁体   中英

How to change c++ standard in Visual Studio 2013

I have been following Bjarne Stroustrup's Book's C++ Programming and Principles 2nd Edition .

My problem is, in his book Bjarne uses some functions from the C++11 standard, which using the following code I discovered I was using c++98.

if (__cplusplus == 201103L) std::cout << "C++11\n";
else if (__cplusplus == 199711L) std::cout << "C++98\n";
else std::cout << "pre-standard C++\n";

When I discovered this, I was using VS 2013 ultimate on a machine with Win8.1 with platform toolset set to v12.0 or higher. I have VS 2010 and VS 2015 (just installed 2015 to see if it fixed the issue) installed on my machine as well.

None of this worked. Beyond the platform toolset being set to v12.0, I have not found any other solutions online. My friend that runs VS 2013 and 2015 on his Mac through parallels does not have this issue.

This thread has the same issue though the OP was using VS 2010: How to "activate" c++11 standard in visual studio 2010?

This thread demostrates that it may be a microsoft issue but still, no solution: https://connect.microsoft.com/VisualStudio/feedback/details/763051/a-value-of-predefined-macro-cplusplus-is-still-199711l

I am completely lost at this point and have no idea how to fix it. Without this standard it makes following Bjarne's book nearly impossible.

How do I change or update my C++ standard?!?!

EDIT / SOLUTION : So the main reason I made this post was while following the book above, I ran into an issue where using initializer-lists were not a recognized feature. According to other posts on this question and links provided, initializer-lists are clearly a feature of C++11 so after I used the above code I assumed I was not using C++11.

Below, are the steps I took to resolve this issue.

  1. First I uninstalled all 2010 redistributables including VS 2010 itself.
  2. Through Programs and Features, I ran VS 2013 uninstall wizard and selected 'repair'
  3. This resolved the issue I was getting about 'initializer-lists' not be recognized, however, there was another error that popped up that the book did not cover.

The error and solution I was getting ended up resulting from an out of date std_lib_facilities.h despite the fact I copied the link straight from the book .PDF

The error and solution I was getting can be found here: Simple Code Need Help - no instance of constructor matches argument list

PS: I have not reinstalled 2010 redistributables since I don't for see me having a need for them at the moment.

Using C++11 in Visual Studio

You cannot "activate or change a certain C++ standard" in Visual Studio. If you want to use macros for conditional compilation you'll have to look up the features you're going to use and the Visual Studio Version supporting it.

If you know the desired version of MSVC you can use something along the lines of

#if _MSC_VER >= 1800
// ...
#endif

VS2010 has #define _MSC_VER 1600 , VS2012 has #define _MSC_VER 1700 and VS2013 has #define _MSC_VER 1800 afaik.

Why doesn't the macro equal 201103L if C++11 is (partially) supported?

Visual Studio doesn't fully support C++11: Supported C++11 features in VS2013 .

Actually the standard says in a note about __cplusplus :

It is intended that future versions of this standard will replace the value of this macro with a greater value. Non-conforming compilers should use a value with at most five decimal digits.

I think the macro is not euqal to 201103L as long as Visual Studio doesn't fully conform to C++11 (if ever ;)).

I belive the macro says 199711L to indicate (almost full) C++98 conformance (although I there are C++98 features left which aren't included in VS).

I am adding this answer per-request. This answer can also be found in OP as an edit.

EDIT / SOLUTION : So the main reason I made this post was while following the book above, I ran into an issue where using initializer-lists were not a recognized feature. According to other posts on this question and links provided, initializer-lists are clearly a feature of C++11 so after I used the above code I assumed I was not using C++11.

Below, are the steps I took to resolve this issue.

First I uninstalled all 2010 redistributables including VS 2010 itself. Through Programs and Features, I ran VS 2013 uninstall wizard and selected 'repair'. This resolved the issue I was getting about 'initializer-lists' not be recognized, however, there was another error that popped up that the book did not cover.

The error and solution I was getting ended up resulting from an out of date std_lib_facilities.h despite the fact I copied the link straight from the book .PDF

The error and solution I was getting can be found here: Simple Code Need Help - no instance of constructor matches argument list

PS: I have not reinstalled 2010 redistributables since I don't for see me having a need for them at the moment.

The issue for the thread you linked is that VS2010 doesn't support many C++11 features, such as std::thread (which is what that op needed).

VS2013 supports most C++11 features. You'll notice that if you make a project and #include <thread> it should work just fine for you.

To see what features of C++11 your compiler supports, check out this: C++11 Core Features Table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM