Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
languagelawyer opened this issue May 5, 2019 · 3 comments
Closed
Labels
bug Something isn't working

Comments

@languagelawyer
Copy link

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.

@andreasfertig
Copy link
Owner

Hello @languagelawyer,

thank you. That is once again an interesting piece of code. I gave it a look and had to scratch my head multiple times to see through it. The sad part is, currently I have no glue how to fix it. I'm willing to choose the "not fixing" card but I will think more about it.

Andreas

@languagelawyer
Copy link
Author

Here is a relevant clang bugreport https://bugs.llvm.org/show_bug.cgi?id=41765

@stale stale bot added the stale No activity label Jul 12, 2019
@andreasfertig andreasfertig removed the stale No activity label Jul 13, 2019
@stale stale bot added the stale No activity label Sep 12, 2019
@andreasfertig andreasfertig removed the stale No activity label Sep 12, 2019
Repository owner deleted a comment from stale bot Sep 12, 2019
Repository owner deleted a comment from stale bot Apr 8, 2020
@andreasfertig andreasfertig added the bug Something isn't working label Apr 8, 2020
@andreasfertig
Copy link
Owner

Hello @languagelawyer,

sorry that it took so long. I starred at this issue multiple times. Until yesterday the conclusion was always "I don't know how to fix this". Then yesterday, I revisited it from a different angle and there it is :-) I overlooked the fact, that this is the first time a lambda is decomposed. I knew it, but didn't take it into account. Lambda FieldDecl does not have a name in Clang. But in other places C++ Insights already makes them visible. So, the fix in the end turn out to be easy. During this, I also corrected the "&&" into a "&" which it really is.

Thanks for reporting this interesting case!

Andreas

andreasfertig added a commit that referenced this issue Apr 8, 2020
Fixed #181: Correctly spell the name of a decomposed closure object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants