【问题标题】:Using c++17 'structured bindings' feature in visual studio 2017在 Visual Studio 2017 中使用 c++17 的“结构化绑定”功能
【发布时间】: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&gt;c:\temp\test\test.cpp(659): error C2064: term does not evaluate to a function taking 0 arguments 1&gt;c:\temp\test\test.cpp(659): error C2119: '&lt;structured binding&gt;': the type for 'auto' cannot be deduced from an empty initializer 1&gt;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


【解决方案1】:
  1. 正如@KerrekSB 在 cmets 中指出的那样,auto [i, f] = s(); 应该是 auto [i, f] = s;
  2. 您说您使用的是 VS2017,但随后指出您的编译器版本是 19.00.24218,实际上是 VS2015 Update 3... VC++ 直到 VS2017 Update 3 才支持结构化绑定(编译器 v19.11.25503)——您需要更新(或修复您的项目设置和/或构建环境,以便使用正确的编译器)。

【讨论】:

  • 是的。虽然我不是在使用 VS 2015 编译器(我错误地列出了该编译器版本),但我意识到 VS 2017 从未更新到最新版本(更新 3)。这样做之后,一切都按预期工作。
【解决方案2】:

只快速浏览了您链接的页面,但似乎您应该这样:

    #include <iostream>

struct S
{
    int i = 0;
    float f = 32.0f;
};

int main()
{
    S s(); <--- You were missing the ()

    auto [i, f] = s();

    std::cin.get();

    return 0;
}

【讨论】:

  • 你尝试编译这个吗? error LNK2019: unresolved external symbol "struct S __cdecl s(void)" (?s@@YA?AUS@@XZ) referenced in function _main 如果我没记错的话,S s(); 是一个函数声明。
  • 在他链接的博客上,yay foo(); 声明了一个名为 foo 的函数,该函数返回 yay。简单地模仿语法不是“问题”;你需要了解发生了什么。一旦你这样做了,那不是问题;他正在尝试获取一些特定版本的 Microsoft 编译器来使用此功能。
  • 我没有。我只看了页面上的示例,并与他的示例进行了比较。
  • 是的,无论我尝试采用哪种方式实现它,我仍然会遇到相同的错误。由于我如何实现事情似乎并不重要,这让我相信它与不支持 c++17 的此功能的编译器有关。要么是那个,要么是我缺少其他东西,比如没有启用特定的编译器开关或其他东西。
  • 这里的大多数示例:en.cppreference.com/w/cpp/language/structured_binding 在 VS2017 中为我工作。 shrug 不确定您到底要做什么,但auto[x, y] = S(); 有效,x, y 对应于结构中初始化的默认值。如果我在链接文章中实现 yay 函数以返回默认构造的结构,它也可以工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-08
  • 2020-08-04
  • 2020-12-27
相关资源
最近更新 更多