How to "activate" c++11 standard in visual studio 2010? - c++

I am new to c++ programming and I need to use the Thread class in my VS 2010 project.
I've found this reference, but when I try the following:
#include <thread>
VS 2010 obviously tells me 'Error: cannot open source file "thread"'. I understand that I need to "activate" c++11 standard somehow. I do not even know where to start.
So what should I do to use () c++11 standard in visual studio 2010?

std::thread is obviously not in VS 2010. I think it was added with VS 2012, which is also supported by this question and answer. Is there any specific reason you're using 2010 rather than the latest version, 2013, which supports far more part of C++11?
Also to note: Contrary to GCC, MSVC doesn't have an "opt-in" for newer standards. It just supports them out of the box as far as implemented.

The Visual C++ compiler is not fully C++11 compatible. C++11 features had been supported since Visual Studio 2010 and added incrementally. Not even the next version of Visual Studio will provide full C++11 compatibility. A matrix of C++11 features available in different versions of Visual Studio can be found here:
C++0x Core Language Features In VC10: The Table
C++11 Features in Visual C++ 11
C++11/14 STL Features, Fixes, And Breaking Changes In VS 2013

C++11 is enabled by default, but there is not many features implemented in VS 2010. C++11 standard library is missing many headers in VS 2010. Here is a comparison of a last few VS releases regarding the C++11 support.

Here's what I've found by myself.
To "activate" c++11 in visual studio you need to set "Platform Toolset" in project->properties to v110 or above. So that's how visual studio will understand that it should use c++11 features.
BUT!
The Visual C++ compiler is not fully C++11 compatible. C++11 features had been supported since Visual Studio 2010 and added incrementally. Not even the next version of Visual Studio will provide full C++11 compatibility.
Marius Bancila
So it worked for <thread> (and <future>) in visual studio 2012.
As I suggest it's impossible to set Platform Toolset above v100 in vs2010, so it's impossible to "activate" c++11 in vs2010.
Conclusion:
to use c++11 standart features in visual studio you will need to use 2012 and higher version which supports Platform Toolset v110 and above.
Correct me please if I'm wrong!

d= (◕‿↼ ) C++11 is enabled by default, But unfortunately, not even "Visual Studio 2017" is fully C++11 compliant.
(I got here while building Boost, which's build section only mentions their need for C++11 compliant compiler, and NOT with what MSVC version they tested Boost.)
Microsoft says:
"Support for C11 and C17 standards is available in Visual Studio 2019 version 16.8 and later"
But I didn't test their claim yet.

Related

What is the latest C++ standard to target Windows XP with Visual Studio?

Visual Studio 2019 seems to have good support for C++17. Unfortunately, it seems binaries built with it require the Universal CRT to be installed on the target machine, and the minimum supported OS for the UCRT is Vista.
So, if I want to build a binary to target Windows XP, must I use a VS C++ compiler preceding the UCRT? Is that VS 2013, which has some support for C++11?
The latest toolset that has Windows XP support is v141_xp, that is the XP toolset from Visual Studio 2017. It has full C++14 support, and partial C++17 support.
It comes with Visual Studio 2019, too:
Unfortunately, it does not have full C++17 and C++20 support.
The latest update of VS2019 has almost complete C++20 support in v142 toolset, and there is an update expected to make it complete, but it is without XP support.
VS2022 drops Vista and support some C++23 in its v143 toolset. It still ships with v141_xp toolset as optional [deprecated] component.
The v141_xp toolset still has the support of C++14, and partial C++17. It mostly corresponds to the Conformance table where they mention VS 2017. (Say, you will have std::any or terse static_assert, but won't be able to use shared_mutex, as it relies on Vista SRWLOCK)
See also: How to install build tools for v141_xp for VC 2017?

Is installing Visual studio 2015 enterprise edition sufficient to work with C++11/C++14?

I want to work with C++ 11 (if not C++ 14). I am installing visual studio 2015 enterprise edition. Is this sufficient?
Basically, yes. Most of C++11 has been implemented in VS2015. Most of C++14 has been implemented in VS2017.
Completely? No.
There are still various missing features (expression SFINAE) and bugs in the Microsoft compiler implementations of C++. If and when you run into these limitations depends on the style of code you write.
You can check that on MSDN or cppreference
A screenshot from MSDN:

Compilation error with "for each" loop in C++ VS2010

I was working on a little C++ project at home, which I brought into school to show my teacher. At home I have Visual Studio 2012, whereas the school computers have Visual Studio 2010. In my code, from home, I had a for loop, like so, which compiled:
for(char c : myStr){...}
However, when I tried it on my school's computers, it did not compile, and I ended up having to do this instead:
for each(char c in myStr){...}
Why is this the case?
The for(char c : myStr){...} syntax is new with C++11, so anything using an older version of C++ won't compile with that syntax.
Previous to C++11, for_each is defined in the algorithm header.
C++11 range-based for loops aren't supported in Visual Studio 2010.
The second form is a syntax that leaked into the compiler from C++/CLI (an entirely different language that targets the .NET runtime). I filled a bug on this a while back. If you compile with the /Za switch, it will disable this language extension. You will need to use the C++03 for loop syntax using an iterator or std::for_each.
The "range for" is a C++ 11 feature that was added in Visual Studio 2012. To learn more about which C++ 11 features are in Visual Studio 2008 (VC9) and Visual Studio 2010 (VC10), check the blog entry from the Visual C++ team. There are similar tables to let you know about Visual Studio 2012 and several different releases of Visual Studio 2013.
Bottom line: your for loop that you did at home is great if you have Visual Studio 2012. If you don't, use a regular for or std::for_each, not the for each you're using there.
Foreach loop was introduced in the C++11 standard, the compilers at your school probably aren't up to date with the new standard.
MS VC++ 2010 was released before the C++ 2011 Standard was adopted. So it does not support the range-based for statement. On the other hand MS VC++ 2010 has MS language extension for-each-in that was introduced in managed C++ to support foreach statement of C#.
The for(char c: myStr) syntax is one of the new C++11 features and VC++ in Visual Studio does not support it.
See this for a list of C++ features which VS2010 and VS2012 C++ compilers implement:
http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx

change C++ version in visual studio

How to check the version of C++ in Visual C++ Express Edition 2010 and change it to C++11?
I've tried to use uniform initialization like std::vector<std::string> v{"Hello" , "World"};, but it doesn't work.
You cannot change the C++ language version used by Visual Studio 2010. It does have partial support for C++11, but since VS 2010 was released before the C++11 standard was finalized, certain features are not exactly in-line with the standard, and furthermore not all features have been implemented.
See this chart for a list of which C++11 features are supported in various major compilers and the versions at which they became available.
Unfortunately, that is not implemented in the compiler yet, not even in VS 2012.
Visual C++ does not support uniform initialization at present time. The most recent compiler CTP release (for Visual Studio 2012, not 2010) has support for it, but that is an alpha-quality product. Moreover, there is no standard library support in there yet, so you still wouldn't be able to use uniform initialization with vector.
(In any case, how would you expect C++2011 to be supported on MSVC++2010?)

Visual express c++ with C++11 and threads support

I am currently using Visual Express C++ (2010) for my project, but I am looking at features in C++11 (thread support). This does not seem to be supported in Express 2010.
From my research so far it looks like I will need Visual Express C++ 2012 version to get support for this (http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx)?
Cite from this page:
In VC11, we intend to completely support the C++11 Standard Library, modulo not-yet-implemented compiler features. (Additionally, VC11
won't completely implement the C99 Standard Library, which has been
incorporated by reference into the C++11 Standard Library. Note that
VC10 and VC11 already have .) Here's a non-exhaustive list
of the changes we're making:
New headers:
<atomic>, <chrono>, <condition_variable>, <future>,<mutex>, <ratio>,
<scoped_allocator>, and <thread>.
So in summary:
What are my options to get Visual Express with <thread> support?
To get support for Visual Studio Express you either need Visual Studio Express 2012, or you can purchase my Just::Thread library, which provides the C++11 thread library for MSVC 2005 and later on Windows, and various versions of gcc across Windows, Linux and MacOSX.