#include <memory>
template <typename T>
class Wrapper {
public:
Wrapper() = delete;
Wrapper(const Wrapper&) = delete;
Wrapper(Wrapper&&) = delete;
~Wrapper() = default;
Wrapper(const T&) = delete;
Wrapper(T&& in) : instance{std::move(in)} {}
T instance;
};
void foo(Wrapper<std::shared_ptr<int>>) {}
int main() {
auto ptr = std::make_shared<int>(1);
foo(std::move(ptr));
}
這一直致力於在C++ 17所以我從來沒有給它認爲,但爲什麼會發生這種代碼嘗試和調用在C++ 14的移動構造函數?它不應該在函數參數中構建嗎?這看起來不是C++ 17的問題,但不是用C++ 14編譯的。複製參數調用構造函數刪除時構造不應該叫
我看到的唯一解決方法是使foo
參數爲右值,但是有什麼我可以做的,使得這項工作不需要在foo
中使參數在C++ 14中成爲一個右值?
我首先想到的會是暫時的必須是構造函數才能被傳遞給函數,但什麼是更令人驚訝的是,即使-fno-elide-constructors
和取消刪除移動構造函數和拷貝構造函數那些不似乎被稱爲!這是gcc和clang中的一個bug嗎?
的錯誤 見https://wandbox.org/permlink/f6sa5Rm3NxZLy5P1,看看奇怪的行爲https://wandbox.org/permlink/Kh6CG4OVbUAjvEZz
什麼是確切的錯誤,你會得到 – vu1p3n0x
@ vu1p3n0x張貼他們的問題,它是在標題以及 – Curious
在你的鏈接,「看到了奇怪的行爲,」它確實呼叫轉移構造 –