<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/58967>58967</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [clang]  Inconsistent behavior of overload resolution with user-defined conversion to rvalue reference
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          s417-lama
      </td>
    </tr>
</table>

<pre>
    When overload resolution for copy/move contructors is concerned with user-defined conversion to rvalue reference, the behavior is inconsistent across different compilers and compilation options.

An example code:
```cpp
#include <cstdio>
#include <utility>
#include <type_traits>

template <typename T>
class foo {
  T& t_;
public:
  foo(T& t) : t_(t) {}
  operator T() const & { return t_; }
  operator T&&() && { return std::move(t_); }
};

class bar {
  int val_;
public:
  bar(int v) : val_(v) {}
  bar(const bar& b) : val_(b.val_) { printf("copy constructed\n"); }
  bar& operator=(const bar& b)     { printf("copy assigned\n"); val_ = b.val_; return *this; }
  bar(bar&& mv) : val_(mv.val_)    { printf("move constructed\n"); mv.val_ = -1; }
  bar& operator=(bar&& mv)         { printf("move assigned\n"); val_ = mv.val_; mv.val_ = -1; return *this; }
};

int main() {
  bar v(1);
  foo<bar> f(v);
  bar v2(std::move(f));
}
```

I think the move constructor should be called with this expression: `bar v2(std::move(f));`, because `operator T&&() &&` in `foo` should be preferred over `operator T() const &`, but it is not true when I use clang with `-std=c++17` option.

Output:
```
⟩ clang++-15 -std=c++17 test.cpp && ./a.out
copy constructed

⟩ clang++-15 -std=c++14 test.cpp && ./a.out
move constructed

⟩ g++ -std=c++14 test.cpp && ./a.out
move constructed

⟩ g++ -std=c++17 test.cpp && ./a.out
move constructed
```

In gcc or clang with `-std=c++14` or lower, move constructor is called.

Used compiler versions:
```
⟩ g++ --version
g++ (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

⟩ clang++-15 --version
Ubuntu clang version 15.0.4-++20221102053308+5c68a1cb1231-1~exp1~20221102053355.92
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy9VltvqzgQ_jXkxQJxCYQ85CFXbaSzbdWmOtqnyoBJvEswsk3a_vudsSG3ppd92QgFbM_lm_H482SieJ_83rGaiAOTlaAFkUyJqtVc1KQUkuSieXfC1R7W4bvWss21kIpwhcOcyZoV5JXrHWkVk27BSo4zsAYGFVrRgsgDrVoGpksmGSg54ZzoHSMZ29EDBy9gjdego7jSrNaE5lIoRQpeGg0N9vYNr8AioXXRjagBKRp8Kc_xF44_tf_TmrA3um8qxFwwJ-rmncS3T9403UwYgeOqLRhxonmudMGFEy1vLUJOKq7fP1nV7w170ZJyrU4S5l8zAEL1Uaqme0Y2R6G8ohBpKQRxRjM7RWA5TIh-caJupmmziufHOAjKO2FqxZxwDLanKB-mdgSWRoteVjRMUtg0tJriMiZaE9QFQdgV3craeiOfqCXmMcp2cK4JSUNk0RSLBCEAjvGFMfzoQzmPOqPyPGoOGw2V8kXYoAAOjFwftVEI08PHuK2wDdZ8JyS70so8-2FUSSPBcmniDLHubaKw4lnhxPMapq8jI73lPltOtLjpFH83fUAe-La-doCwAOiCdAijY7adcKp3XN1CkVqH6HN_nZ_94RjqDST98b4dbadr8LjBT-K_BtL_bvr9JgM98ptAvkjKx6LDutlTXveVfKo8LEQAmgbW-fkpi-YYTLQkZVdlZ-tGDQCn10egRLkz0ROonoHOca2BDHn9j6HEy52A06d2oq0KoEqS06rqyRajBY5rgK2RZHGjwehP8IBrYN-M5RQIG5W-POewDscSxTAXMDjBaQydS0CEd8e1pUui6b22mnCNbF8LTSBCRl7x8lnj5UGAE-qtDQ_kXRPDInfCGTzBCJ1brr-g-vtWN63-y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