Skip to content

Incorrect move-only lambda code generation #605

@samwhitlock

Description

@samwhitlock

I tried to compile this code with C++23 to see move_only_function working:

#include <cstdio>
#include <functional>
#include <memory>

using namespace std;

int main()
{
    std::move_only_function<void(void)> a1 = [a=make_unique<int>(42)]() {};
}

but I noticed that the code generated for the lambda itself is incorrect. Here is the full output

#include <cstdio>
#include <functional>
#include <memory>

using namespace std;

int main()
{
    
  class __lambda_9_46
  {
    public: 
    inline /*constexpr */ void operator()() const
    {
    }
    
    private: 
    std::unique_ptr<int, std::default_delete<int> > a;
    public: 
    // inline __lambda_9_46(const __lambda_9_46 &) /* noexcept */ = delete;
    // inline /*constexpr */ __lambda_9_46(__lambda_9_46 &&) noexcept = default;
    // inline __lambda_9_46 & operator=(const __lambda_9_46 &) /* noexcept */ = delete;
    __lambda_9_46(const std::unique_ptr<int, std::default_delete<int> > & _a)
    : a{_a}
    {}
    
  };
  
  std::move_only_function<void ()> a1 = std::move_only_function<void ()>(__lambda_9_46{std::make_unique<int>(42)});
  return 0;
}

The constructor for this lambda should take the argument by r-value reference, because otherwise this code cannot compile.

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