在Visual Studio 2017中使用C ++ 17``结构化绑定''功能 [英] Using c++17 'structured bindings' feature in visual studio 2017
问题描述
我正在尝试在代码中利用某些c ++ 17功能(例如结构化绑定),但是编译器会不断给我错误,而且我不确定这是因为我做错了还是没有做正确安装c ++ 17以在VS17中工作。我尝试编译的简单代码是:
I'm trying to utilize some c++17 features such as structured bindings in my code but the compiler keeps giving me errors and I'm not sure if it's because I'm doing things wrong or if I haven't setup c++17 properly to work in VS17. The simple code I'm trying to compile is this:
#include <iostream>
struct S
{
int i = 0;
float f = 32.0f;
};
int main()
{
S s;
auto [i, f] = s();
std::cin.get();
return 0;
}
从这篇文章,这就是我将如何使用新的c ++ 17语法返回多个值的方法。但是,我不断收到这些错误:
From the understanding of this article, this is how I would use the new c++17 syntax to return multiple values. However, I keep getting these errors:
c:\users\jason\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(16): error C2059: syntax error: 'empty declaration'
1>c:\users\jason\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(16): error C2143: syntax error: missing ';' before '['
1>c:\users\jason\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(16): warning C4467: usage of ATL attributes is deprecated
1>c:\users\jason\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(16): error C2337: 'i': attribute not found
1>c:\users\jason\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(16): error C2337: 'f': attribute not found
1>c:\users\jason\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(16): error C2059: syntax error: '='
我还尝试在项目属性内将编译器开关设置为std:/ c ++ latest,但仍然没有骰子。我在做什么错了?
I've also tried setting the compiler switch to std:/c++latest inside the properties of the project but still no dice. What I'm I doing wrong?
推荐答案
- 正如@KerrekSB在评论中指出的那样,
auto [i,f] = s();
应该是auto [i,f] = s;
。 - 您说您正在使用VS2017,但随后指出您的编译器版本为19.00.24218,实际上是VS2015 Update 3 ... VC ++在VS201 7 <之前不支持结构化绑定。 / em> Update 3(编译器v19.11.25503)—您需要更新(或修复项目设置和/或构建环境,以便使用正确的编译器)。
- As @KerrekSB noted in the comments,
auto [i, f] = s();
should beauto [i, f] = s;
. - You said that you're using VS2017 but then indicated that your compiler version is 19.00.24218, which is actually VS2015 Update 3... VC++ did not support structured bindings until VS2017 Update 3 (compiler v19.11.25503) — you need to update (or fix your project settings and/or build environment so that the correct compiler is used).
这篇关于在Visual Studio 2017中使用C ++ 17``结构化绑定''功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!