Skip to content

[Clang]: clang rejects valid codes when it trying capturing something never occuring in lambda body #95735

Closed
@RungeCC

Description

@RungeCC

struct A {
  void operator()(int) {}
};
inline constexpr struct {
  constexpr auto operator()(A fn) const
  {
    return [f{fn}](auto &&tpl) 
        noexcept(noexcept(&f))
    { return true; };
  }
} error;

inline constexpr struct {
  constexpr auto operator()(A fn) const
  {
    return [f{fn}](auto &&tpl) 
        // noexcept(noexcept(&f))
    { return true; };
  }
} no_error;



int main() {
  (void)error( A{} )(1);    // $1
  (void)no_error( A{} )(1); // $2

}
  • clang trunk with flag c++26
  • https://godbolt.org/z/1EK6qf8Th
    • see no_error and error function object, if I delete the noexcept clause then everything is OK.
    • clang tells me to capture fn, which is weird (since fn is never used in function body), but if I do so, clang accepts it.
    • if I change f inside the noexcept to fn, then again, everything would be fine.

clang rejects $1 it with:

<source>:8:15: error: variable 'fn' cannot be implicitly captured in a lambda with no capture-default specified
    8 |     return [f{fn}](auto &&tpl) 
      |               ^
<source>:8:12: note: in instantiation of exception specification for 'operator()<int>' requested here
    8 |     return [f{fn}](auto &&tpl) 
      |            ^
<source>:26:21: note: in instantiation of function template specialization '(anonymous struct)::operator()(A)::(anonymous class)::operator()<int>' requested here
   26 |   (void)error( A{} )(1);
      |                     ^
<source>:6:31: note: 'fn' declared here
    6 |   constexpr auto operator()(A fn) const
      |                               ^
<source>:8:12: note: lambda expression begins here
    8 |     return [f{fn}](auto &&tpl) 
      |            ^
<source>:8:12: note: capture 'fn' by value
    8 |     return [f{fn}](auto &&tpl) 
      |            ^
      |            fn
<source>:8:12: note: capture 'fn' by reference
    8 |     return [f{fn}](auto &&tpl) 
      |            ^
      |            &fn
1 error generated.

but accepts $2.

Expected behavior

Accepts both of them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"confirmedVerified by a second partylambdaC++11 lambda expressionsrejects-valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions