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

[expr.prim.lambda.closure] Conversion to function pointer doesn't account for explicit object parameter CWG2561 #5294

Open
brevzin opened this issue Feb 14, 2022 · 6 comments
Labels
cwg Issue must be reviewed by CWG. not-editorial Issue is not deemed editorial; the editorial issue is kept open for tracking.

Comments

@brevzin
Copy link
Contributor

brevzin commented Feb 14, 2022

Paragraphs 8 and 9 talk about what kind of function pointer a capture-less lambda is convertible to:

... has a conversion function to pointer to function with C++ language linkage having the same parameter and return types as the closure type's function call operator.

and

... The conversion function template has the same invented template parameter list, and the pointer to function has the same parameter types, as the function call operator template.

Those aren't quite right in the case of an explicit object parameter. Since this should probably work:

using P = int(*)(int);

auto f = [](this P, int i) { return i; };
auto g = [](this auto, int i) { return i; };

P pf = f;
P pg = g;

For the non-generic case, something like this:

- having the same parameter and return types as the closure type's function call operator
+ having the same parameter types (excluding the explicit object parameter, if any) and return type as the closure type's function call operator

Though wording this for the generic case is trickier since we need to skip the invented template parameter of the explicit object parameter, which might hypothetically also be used by some other parameter?

@cpplearner
Copy link
Contributor

IIUC the explicit object parameter could be used in the lambda-expression. E. g.

struct C {
    C(auto);
    void f();
};

auto lambda = [](this C c) { c.f(); }; // OK?

IIUC if the conversion function excludes the explicit object parameter, then the above lambda would be convertible to void(*)(), and the resulting function pointer would point to a function that calls c.f() for some nonexistent c. I don't think this makes sense.

@jensmaurer
Copy link
Member

Sounds like non-editorial CWG issue material to me.

@jensmaurer jensmaurer added the cwg Issue must be reviewed by CWG. label Feb 14, 2022
@brevzin
Copy link
Contributor Author

brevzin commented Feb 14, 2022

Sounds like non-editorial CWG issue material to me.

Yeah, definitely. I just wasn't sure if you preferred me emailing Core or posting it here.

And per @cpplearner's comment, the wording may not even be wrong 😄.


Yeah, with some more thought, [](this C c) { c.f(); } has to be a void(*)(C), which I think means that the wording is correct.

@cmeerw
Copy link
Contributor

cmeerw commented Apr 1, 2022

But I think we still need to change

The value returned by this conversion function is the address of a function F that, when invoked, has the same effect as invoking the closure type's function call operator on a default-constructed instance of the closure type

as it's not clear what that would mean, e.g.

struct C {
    C(auto) { }
};

void foo() {
    auto l = [](this C) { return 1; };
    int (*fp)(C) = l; // EDIT: originally incorrectly had void as return type
    fp(1); // same effect as decltype(l){}() or decltype(l){}(1) ?
}

(fp should instead just be the address of the function call operator?)

@jensmaurer
Copy link
Member

Why is fp a pointer to function returning void when the lambda returns int?

@jensmaurer
Copy link
Member

CWG2561

@jensmaurer jensmaurer changed the title [expr.prim.lambda.closure] Conversion to function pointer doesn't account for explicit object parameter [expr.prim.lambda.closure] Conversion to function pointer doesn't account for explicit object parameter CWG2561 Apr 2, 2022
@jensmaurer jensmaurer added the not-editorial Issue is not deemed editorial; the editorial issue is kept open for tracking. label Apr 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cwg Issue must be reviewed by CWG. not-editorial Issue is not deemed editorial; the editorial issue is kept open for tracking.
Projects
None yet
Development

No branches or pull requests

4 participants