Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using c++17 'structured bindings' feature in visual studio 2017

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;
}

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: '='

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?

like image 788
Jason Avatar asked Aug 01 '26 15:08

Jason


1 Answers

  1. As @KerrekSB noted in the comments, auto [i, f] = s(); should be auto [i, f] = s;.
  2. 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).
like image 69
ildjarn Avatar answered Aug 04 '26 06:08

ildjarn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!