1

I'm looking at some code that doesn't seem to agree with VS2015, but should

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.32002.185
MinimumVisualStudioVersion = 10.0.40219.1
//array defined first
std::vector<ResizeCtrls> m_controls;
//elsewhere later this whole line:
const auto& [hWnd, resizeType, origSize] = m_controls[i];

aside from the errors indicated by the IDE about the bracketed non-constant args, you know errors in an ostensibly working project you just loaded in the IDE and made no changes to,

hWnd (undefined), resizeType (can't deduce 'auto' type, 'initializer required')

..I don't see what this nameless, anonymous thing on the left means. A left handed initialization/allocation of some sort? If it were an arg to a function, would make some sense to me. I spent a fair amount of time reading up on the auto keyword, and everything I saw had something like

auto variablename[...

I know it had to compile for the author, but the logic defies me.

Phil G
  • 39
  • 1
  • 6
  • The code is utilizing [Structured Bindings](https://en.cppreference.com/w/cpp/language/structured_binding), which is a language feature introduced in C++17. It is taking the individual fields of a `ResizeCtrls` instance and binding them to separate `const-reference` variables `hWnd`, `resizeType`, and `origSize` in a single assignment statement, where each variable has an `auto`-deduced type based on which field is being assigned to it. Is that what you are asking about? – Remy Lebeau Aug 26 '22 at 00:26
  • Yes, that's what was bugging me. Now I don't understand why the .sln file had MinimumVisualStudioVersion = 10.0.40219.1 Thanks – Phil G Aug 26 '22 at 00:37
  • MinimumVisualStudioVersion just mentions the oldest VS version that can understand this file. Older versions can stop reading just there. – BoP Aug 26 '22 at 09:53
  • BOP, So I guess the idea is that IDE can't always know what it doesn't know, right? – Phil G Aug 27 '22 at 01:08

1 Answers1

0
const auto& [hWnd, resizeType, origSize] = m_controls[i];

This is C++17 feature known as "structured binding". In its simplest form it looks like:

auto [identifiers] = object;

You can modify the auto with const, volatile, or a reference &.

You can have 1 or more identifiers inside the brackets, but they must match the number of accessible members that are in object.

In terms of your error, I have seen MSVC throw errors like this if one of the types it is dealing with is incomplete - as in, it has been "forward declared" and its full definition is not visible. Make sure you are including the required headers for ResizeCtrls, hWnd, resizeType, and origSize.

Further reading: https://en.cppreference.com/w/cpp/language/structured_binding

DXPower
  • 391
  • 1
  • 11
  • Thanks. Knowing what something is called, "structured binding", is, well, some fraction of the battle. I don't think I'm going to try and rework it for VS2015. This was in a utility from "Stefan's Tools" I downloaded the source to, and wanted to rework it to use the COPYDATASTRUCT for sending data on the WM_COPYDATA message, quite an omission imho, but to heck with that I'll just make my own little MFC util now. – Phil G Aug 26 '22 at 00:49