Skip to content

Closure object structured binding: data member names are missing #181

@languagelawyer

Description

@languagelawyer

Let's look a the following code

int main()
{
	int x = 1, y = 2;
	auto f = [&](auto self) {
		(void)x; (void)y; // make 'em used!
		auto& [a, b] = *self; // a == 1, b == 2. Depends on the previous line.
		return b - a;
	};
	return f(&f); // returns 1
}

it is transformed into

int main()
{
  int x = 1;
  int y = 2;
    
  class __lambda_4_11
  {
    int & x;
    int & y;
    public: inline /*constexpr */ int operator()(__lambda_4_11 * self) const
    {
      static_cast<void>(x);
      static_cast<void>(y);
      __lambda_4_11 & __self6 = *self;
      int && a = __self6.;
      int && b = __self6.;
      return b - a;
    }
    public: __lambda_4_11(int & _x, int & _y)
    : x{_x}
, y{_y}
    {}
    
  };
  
  __lambda_4_11 f = __lambda_4_11{x, y};
  return f.operator()(&f);
}

https://cppinsights.io/lnk?code=aW50IG1haW4oKQp7CglpbnQgeCA9IDEsIHkgPSAyOwoJYXV0byBmID0gWyZdKGF1dG8gc2VsZikgewoJCSh2b2lkKXg7ICh2b2lkKXk7IC8vIG1ha2UgJ2VtIHVzZWQhCgkJYXV0byYgW2EsIGJdID0gKnNlbGY7IC8vIGEgPT0gMSwgYiA9PSAyLiBEZXBlbmRzIG9uIHRoZSBwcmV2aW91cyBsaW5lLgoJCXJldHVybiBiIC0gYTsKCX07CglyZXR1cm4gZigmZik7IC8vIHJldHVybnMgMQp9&insightsOptions=&std=undefined&rev=1.0

In the lines 15 and 16:

      int && a = __self6.;
      int && b = __self6.;

data member names are missing. Also, I believe, the types of a and b should be int&, not int&&:

      int & a = __self6.x;
      int & b = __self6.y;

Not fixing this is also an option, because it is believed that closure object decomposition should be banned by the Standard.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions