【发布时间】:2017-09-30 22:18:12
【问题描述】:
我正在尝试在我的代码中使用一些 c++17 功能,例如结构化绑定,但编译器不断给我错误,我不确定是因为我做错了还是我没有做错正确设置 c++17 以在 VS17 中工作。我试图编译的简单代码是这样的:
#include <iostream>
struct S
{
int i = 0;
float f = 32.0f;
};
int main()
{
S s;
auto [i, f] = s();
std::cin.get();
return 0;
}
根据this文章的理解,这就是我将如何使用新的c++17语法来返回多个值。但是,我不断收到这些错误:
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 但仍然没有骰子。我做错了什么?
【问题讨论】:
-
你有什么实际版本的编译器? IE。运行
cl /Bv得到什么版本? -
auto [i, f] = s;,非? -
它告诉我版本 19.00.24218.2。我也试过
auto[i,f] = s;,但没有运气。 -
“我也试过
auto[i,f] = s;” ...支持结构化绑定的假设编译器会编译它,但应该将auto[i,f] = s();视为错误,因为s()是无稽之谈。我认为您误解了您链接到的文章。 -
我在 VS2017 中遇到的错误与您不同。
1>c:\temp\test\test.cpp(659): error C2064: term does not evaluate to a function taking 0 arguments 1>c:\temp\test\test.cpp(659): error C2119: '<structured binding>': the type for 'auto' cannot be deduced from an empty initializer 1>c:\temp\test\test.cpp(659): error C3617: initializers of structured bindings must be of array or non-union class type; type 'int' is not permitted
标签: c++ visual-studio c++17