2

W.F. gave a now-deleted answer to my question here which used the line:

auto [x, y] = div_t{ 1, 0 };

From the code in the answer it looks like that's like a tie for the div_t struct. I was hoping that someone could explain what was going on here. The complete function code was as follows:

constexpr bool first_quot() {
    auto [x, y] = std::div_t{1, 0};
    (void)y;
    return x;
}
Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • 5
    That is called [*structured bindings*](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0217r2.html). And yes it works similar to `tie` (but with automatic declaration, definition and initialization all in one go). – Some programmer dude Jan 10 '17 at 13:03
  • I realized that the answer of hvd actually covered fully your question :) As for the syntax it is [structured binding](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0144r0.pdf) – W.F. Jan 10 '17 at 13:03
  • @Someprogrammerdude not entirely as `tie` as it is actually declaration of variables `x` and `y` and `std::tie` binds references of already existing variables and I'm not sure if `tie` can bind these variables to structure - as far as I know it can only tie these variables to `std::tuple` elements. Or am I wrong? – W.F. Jan 10 '17 at 13:06
  • @W.F. Updated my comment. And your right about `std::tie` only being able to handle `std::tuple` (or `std::pair`, because that's what the assignment operator of `std::tuple` handles and `std::tie` returns a `std::tuple`). – Some programmer dude Jan 10 '17 at 13:09
  • 1
    Oh one more thing ;) actually my answer was wrong with using structure binding... my answer should more look like `constexpr bool first_quot() { return std::div_t{1, 0}.quot; }` (so no c++17 even needed) to work according to my intention :) But as it is deleted anyway I haven't edited it :) – W.F. Jan 10 '17 at 13:23
  • @W.F. The reason that I was excited by your answer, was that I understood it to use a `constexpr` if so I could actually use that to select the `generate` statement that I executed allowing me to use the curly brace initialization that I was trying to in the first place. – Jonathan Mee Jan 10 '17 at 13:41
  • 1
    @Someprogrammerdude Any chance I could convince you to source that information and type it up as an answer? When I look up [`auto`](http://en.cppreference.com/w/cpp/language/auto) I don't see that as a potential syntax. I'd sure like the opportunity to read up and understand what's going on here. – Jonathan Mee Jan 10 '17 at 13:44
  • I *have* seen it on cppreference, but can't seem to find it now. :/ – Some programmer dude Jan 10 '17 at 13:49
  • @JonathanMee I'm glad it was helpful :) – W.F. Jan 10 '17 at 13:54
  • 1
    Now when I know the actual terms in the specification, I find references *everywhere*, including [on cppreference](http://en.cppreference.com/w/cpp/language/declarations#Decomposition_declaration)... :) – Some programmer dude Jan 10 '17 at 13:59
  • @Someprogrammerdude Yeah: http://en.cppreference.com/w/cpp/language/declarations#Decomposition_declaration – Jonathan Mee Jan 10 '17 at 14:05
  • @W.F. So what's the point of the `(void)y` is that just to prevent unused variable warnings? – Jonathan Mee Jan 10 '17 at 16:24
  • 1
    @JonathanMee exactly. Unfortunetly I don't think that you can use structure binding (or however it is called now) with unnamed variable... – W.F. Jan 10 '17 at 18:12
  • @W.F. I found a way to write a C++11 helper function without direct member assignment! Check it out: http://stackoverflow.com/a/41592619/2642059 I'm pretty excited, except for the fact that it doesn't work on Visual Studio :( – Jonathan Mee Jan 11 '17 at 15:21

1 Answers1

8

In the most most recent draft of the C++17 specification it's called "Decomposition declarations" and is defined in section 8.5 [dcl.decomp].

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621