简体   繁体   English

如何在 Visual Studio 2013 中更改 C++ 标准

[英]How to change c++ standard in Visual Studio 2013

I have been following Bjarne Stroustrup's Book's C++ Programming and Principles 2nd Edition .我一直在关注 Bjarne Stroustrup 的书的C++ 编程和原理第二版

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.我的问题是,在他的书中 Bjarne 使用了 C++11 标准中的一些函数,使用以下代码我发现我使用的是 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.当我发现这一点时,我正在一台装有 Win8.1 且平台工具集设置为 v12.0 或更高版本的机器上使用 VS 2013 Ultimate。 I have VS 2010 and VS 2015 (just installed 2015 to see if it fixed the issue) installed on my machine as well.我的机器上也安装了 VS 2010 和 VS 2015(刚刚安装了 2015 以查看它是否解决了问题)。

None of this worked.这些都没有奏效。 Beyond the platform toolset being set to v12.0, I have not found any other solutions online.除了将平台工具集设置为 v12.0 之外,我还没有在网上找到任何其他解决方案。 My friend that runs VS 2013 and 2015 on his Mac through parallels does not have this issue.我的朋友通过并行在他的 Mac 上运行 VS 2013 和 2015 没有这个问题。

This thread has the same issue though the OP was using VS 2010: How to "activate" c++11 standard in visual studio 2010?尽管 OP 使用的是 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该线程表明它可能是微软的问题,但仍然没有解决方案: 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.没有这个标准,几乎不可能遵循 Bjarne 的书。

How do I change or update my C++ standard?!?!如何更改或更新我的 C++ 标准?!?!

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.根据关于这个问题的其他帖子和提供的链接,初始化列表显然是 C++11 的一个特性,所以在我使用上面的代码之后,我假设我没有使用 C++11。

Below, are the steps I took to resolve this issue.下面是我为解决此问题而采取的步骤。

  1. First I uninstalled all 2010 redistributables including VS 2010 itself.首先,我卸载了所有 2010 可再发行组件,包括 VS 2010 本身。
  2. Through Programs and Features, I ran VS 2013 uninstall wizard and selected 'repair'通过程序和功能,我运行了 VS 2013 卸载向导并选择了“修复”
  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尽管我直接从书中复制了链接,但我得到的错误和解决方案最终是由过时的std_lib_facilities.h引起的。

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. PS:我没有重新安装 2010 可再发行组件,因为我目前不认为我需要它们。

Using C++11 in Visual Studio在 Visual Studio 中使用 C++11

You cannot "activate or change a certain C++ standard" in Visual Studio.您不能在 Visual Studio 中“激活或更改某个 C++ 标准”。 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.如果要使用宏进行条件编译,则必须查找要使用的功能以及支持它的 Visual Studio 版本。

If you know the desired version of MSVC you can use something along the lines of如果您知道所需的 MSVC 版本,则可以使用以下内容

#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. VS2010 有#define _MSC_VER 1600 ,VS2012 有#define _MSC_VER 1700和 VS2013 有#define _MSC_VER 1800 afaik。

Why doesn't the macro equal 201103L if C++11 is (partially) supported?如果(部分)支持 C++11,为什么宏不等于201103L

Visual Studio doesn't fully support C++11: Supported C++11 features in VS2013 . Visual Studio 不完全支持 C++11: Supported C++11 features in VS2013

Actually the standard says in a note about __cplusplus :实际上标准在关于__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 ;)).我认为只要 Visual Studio 不完全符合 C++11(如果有的话;)),这个宏就不201103L201103L

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).我相信宏说199711L表示(几乎完整的)C++98 一致性(尽管我还剩下 C++98 特性,它们没有包含在 VS 中)。

I am adding this answer per-request.我正在根据请求添加此答案。 This answer can also be found in OP as an edit.这个答案也可以在 OP 中作为编辑找到。

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.根据关于这个问题的其他帖子和提供的链接,初始化列表显然是 C++11 的一个特性,所以在我使用上面的代码之后,我假设我没有使用 C++11。

Below, are the steps I took to resolve this issue.下面是我为解决此问题而采取的步骤。

First I uninstalled all 2010 redistributables including VS 2010 itself.首先,我卸载了所有 2010 可再发行组件,包括 VS 2010 本身。 Through Programs and Features, I ran VS 2013 uninstall wizard and selected 'repair'.通过程序和功能,我运行了 VS 2013 卸载向导并选择了“修复”。 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尽管我直接从书中复制了链接,但我得到的错误和解决方案最终是由过时的std_lib_facilities.h引起的。

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. PS:我没有重新安装 2010 可再发行组件,因为我目前不认为我需要它们。

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).您链接的线程的问题是 VS2010 不支持许多 C++11 功能,例如 std::thread (这是操作所需要的)。

VS2013 supports most C++11 features. VS2013 支持大部分 C++11 特性。 You'll notice that if you make a project and #include <thread> it should work just fine for you.您会注意到,如果您创建一个项目并使用#include <thread>它应该可以正常工作。

To see what features of C++11 your compiler supports, check out this: C++11 Core Features Table要查看您的编译器支持 C++11 的哪些特性,请查看: C++11 核心特性表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改 Visual Studio 2019 中的默认 C++ 语言标准? - How to change default C++ language standard in Visual Studio 2019? 如何在Visual Studio中更改C ++标准库实现 - How to change C++ standard library implementation in Visual Studio 是否可以使用Visual Studio 2013 C ++强制执行标准行为? - Is it possible to enforce standard behavior with Visual Studio 2013 C++? 如何在Visual Studio 2013中更改我的C ++应用程序的名称(该名称显示在窗口顶部)? - How can I change the name of my C++ application in Visual Studio 2013 (The name that appears at the top of the window)? 创建新项目时,如何更改Visual Studio 2013(C ++)中的默认#include? - How to change default #includes in Visual Studio 2013(C++) when I create a new project? 如何更改 Visual Studio 中所有项目的默认语言标准。 C++ - How to change the default language standard for all projects in Visual Studio. C++ 如何使Visual Studio 2013不停止错误构建(C ++) - How to get Visual Studio 2013 to not stop the build on errors (C++) 如何在Visual Studio 2013,C ++中添加头文件 - How to add a header file in Visual Studio 2013, C++ 如何在Visual Studio 2013中的C ++项目中使用libcurl - How to use libcurl in a C++ project in Visual Studio 2013 如何在C ++ Visual Studio 2013中将列表元素转换为文本块 - How to convert list element to textblock in C++ visual studio 2013
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM