Skip to content

[Clang] Deducing this does not work correctly with move-only types. #100341

@slavek-kucera

Description

@slavek-kucera

Move only types have to be explicitly cast to r-value for the move constructor to be used. https://godbolt.org/z/fabdhKKEj

#include <utility>

struct X {
    X() = default;
    X(X&&) = default;
    void operator()(this X);
};

void fail() {
    X()();
    [x = X{}](this auto) {}();
}
void pass() {
    std::move(X())();
    std::move([x = X{}](this auto) {})();
}
<source>:10:5: error: call to implicitly-deleted copy constructor of 'X'
   10 |     X()();
      |     ^~~
<source>:5:5: note: copy constructor is implicitly deleted because 'X' has a user-declared move constructor
    5 |     X(X&&) = default;
      |     ^
<source>:6:27: note: passing argument to parameter here
    6 |     void operator()(this X);
      |                           ^
<source>:11:5: error: call to implicitly-deleted copy constructor of '(lambda at <source>:11:5)'
   11 |     [x = X{}](this auto) {}();
      |     ^~~~~~~~~~~~~~~~~~~~~~~
<source>:11:6: note: copy constructor of '(lambda at <source>:11:5)' is implicitly deleted because field '' has a deleted copy constructor
   11 |     [x = X{}](this auto) {}();
      |      ^
<source>:5:5: note: copy constructor is implicitly deleted because 'X' has a user-declared move constructor
    5 |     X(X&&) = default;
      |     ^
<source>:11:24: note: passing argument to parameter here
   11 |     [x = X{}](this auto) {}();
      |                        ^

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions