C++11 在 Visual Studio 2017 中可用吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47043869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:41:42  来源:igfitidea点击:

Is C++11 available in Visual Studio 2017?

c++c++11visual-studio-2017

提问by user3344484

I am currently using Visual Studio Community 2017. From looking at the C++ Language Standards in the project properties, they only provide C++14 and C++17. Since my code was completed for a previous assignment using a compiler for C++11, I am unable to run my code using functions such as stoi. My question is if there is a way to add C++11 to the language standards for C++?

我目前使用的是 Visual Studio Community 2017。从项目属性中的 C++ Language Standards 来看,他们只提供 C++14 和 C++17。由于我的代码是使用 C++11 的编译器完成的先前分配的代码,因此我无法使用诸如 stoi 之类的函数运行我的代码。我的问题是是否有办法将 C++11 添加到 C++ 的语言标准中?

I am creating a DLL for a GUI, my initializations are:

我正在为 GUI 创建一个 DLL,我的初始化是:

#include <string>
#include "stdafx.h"

using namespace std;

Here I am creating a fraction class, the main errors follow in the ifstream:

在这里,我创建了一个分数类,主要错误出现在 ifstream 中:

istream& operator>>(istream& in, Fraction& f) {

string number;
in >> number;                           //read the number

size_t delimiter = number.find("/");    //find the delimiter in the string "/"

if (delimiter != string::npos) {            //if delimiter is not empty

    int n = stoi(number.substr(0, delimiter));      //set numerator from string to integer before the "/"
    int d = stoi(number.substr(delimiter + 1));     //set denominator from string to integer after the "/"

    if (d == 0) { //if denominator is 0
        throw FractionException("Illegal denominator, cannot divide by zero.");  //illegal argument throw
    }
    else if (n == 0 && d != 0) {    //numerator is 0, then set values as zero fraction
        f.numVal = 0;
        f.denVal = 1;
    }
    else {                      //set the values into the fraction and normalize and reduce fraction to minimum
        f.numVal = n;
        f.denVal = d;

        f.normalizeAndReduce(f.numVal, f.denVal);
    }
}
else {  //else if there is no delimiter it would be a single integer
    f.numVal = stoi(number);
    f.denVal = 1;
}

return in;
}

I am getting the following errors:

我收到以下错误:

C2679: binary '>>': no operator found which takes a right-hand operator of type 'std::string"
C3861: 'stoi' identifier not found

This method worked perfectly fine in eclipse, not sure what I am doing wrong.

这种方法在 eclipse 中工作得很好,不知道我做错了什么。

回答by Chuck Walbourn

The Visual C++ 2017 compiler is C++11/C++14 compliant with a few specific exceptions:

Visual C++ 2017 编译器符合 C++11/C++14,但有一些特定的例外:

  • Expression SFINAEis implemented, but not complete.
  • Full C99 preprocessor support is limited due to some bugs with variadic macros
  • Two phase name lookup is in VS 2017 (15.3 update) but is incompleteand only active when using /permissive-
  • 表达式 SFINAE已实现,但尚未完成。
  • 由于可变参数宏的一些错误,完整的 C99 预处理器支持受到限制
  • 两阶段名称查找在 VS 2017(15.3 更新)中,但不完整且仅在使用/permissive-时才处于活动状态

The compiler does not offer a specific C++11 mode and defaults to C++14, but that standard is fully inclusive of C++11. C++17 support is in progress, and requires you use the /std:c++17or /std::c++latestswitch.

编译器不提供特定的 C++11 模式并默认为 C++14,但该标准完全包含 C++11。C++17 支持正在进行中,需要您使用/std:c++17/std::c++latest开关。

std::stoirequires you include the appropriate header, specifically <string>>Either you forgot to include that header -or- you didn't deal with the namespaceresolution (either explicitly as std::or via using namespace std;)

std::stoi要求您包含适当的标题,特别是<string>>您忘记包含该标题 - 或者 - 您没有处理namespace分辨率(明确作为std::或通过using namespace std;

See C++17 Features And STL Fixes In VS 2017 15.3for the latest status of C++11/C++14/C++17 standards conformance as of the VS 2017 (15.3 update)

请参阅VS 2017 15.3中的C++17 特性和 STL 修复,了解截至 VS 2017(15.3 更新)的 C++11/C++14/C++17 标准一致性的最新状态

UPDATED:Now that you have posted your code, I see that the problem has nothingto do with which standard is supported. Your problem is that you don't know the secrets of how Precompiled Headers work.

更新:既然您已经发布了代码,我发现问题与支持的标准无关。你的问题是你不知道预编译头如何工作的秘密。

Change:

改变:

#include <string>
#include "stdafx.h"

to:

到:

#include "stdafx.h"
#include <string>

-or- add #include <string>to the precompiled header stdafx.hdirectly.

- 或 -直接添加#include <string>到预编译头stdafx.h

See Creating Precompiled Header Files

请参阅创建预编译头文件

回答by AliReza

Microsoft I think has declared that

我认为微软已经声明

Note that there is no plan to add a C++11 switch. link

请注意,没有计划添加 C++11 开关。关联

So there's not an explicit switch

所以没有明确的开关

回答by Mike Diack

As an update to this, VS 2017, Update 9.4 (releasd Dec 2018) is now fully C++17 compliant.

作为对此的更新,VS 2017 更新 9.4(2018 年 12 月发布)现在完全符合 C++17。

回答by davidbak

C++ is an evolving standard - after 2003 there was 2011 (C++11) then 2014 (C++14) and now we have 2017 (C++17) and we're working towards 2020 (C++20). They're upward compatible.

C++ 是一个不断发展的标准 - 2003 年之后是 2011 年(C++11)然后是 2014 年(C++14),现在我们有了 2017 年(C++17),我们正朝着 2020 年(C++20)努力。它们向上兼容。

Look herefor Microsoft Visual C++ support for the various standards.

看看这里为各种标准的Microsoft Visual C ++的支持。