-
-
Save Irfy/666c68d0d4c973a12bdf to your computer and use it in GitHub Desktop.
Demoing defaulted move ctor neither being generated nor called (see ImplicitDelete)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <utility> | |
#include <iostream> | |
using namespace std; | |
struct Explicit { | |
// prints whether the containing class's move or copy constructor was called | |
// in practice this would be the expensive vector<string> | |
string owner; | |
Explicit(string owner) : owner(owner) {}; | |
Explicit(const Explicit& o) { cout << o.owner << " is actually copying\n"; } | |
Explicit(Explicit&& o) noexcept { cout << o.owner << " is moving\n"; } | |
}; | |
struct ExplicitDelete { | |
ExplicitDelete() = default; | |
ExplicitDelete(const ExplicitDelete&) = default; | |
ExplicitDelete(ExplicitDelete&&) noexcept = delete; | |
}; | |
struct ImplicitDelete : ExplicitDelete { | |
Explicit exp{"ImplicitDelete"}; | |
ImplicitDelete() = default; | |
ImplicitDelete(const ImplicitDelete&) = default; | |
ImplicitDelete(ImplicitDelete&&) = default; | |
}; | |
struct Implicit : ImplicitDelete { | |
Explicit exp{"Implicit"}; | |
}; | |
int main() { | |
ImplicitDelete id1; | |
ImplicitDelete id2(move(id1)); // expect copy call | |
Implicit i1; | |
Implicit i2(move(i1)); // expect 1x ImplicitDelete's copy and 1x Implicit's move | |
return 0; | |
} | |
------------------------------------ | |
$ vim x.cc; CXXFLAGS="-std=gnu++11 -O3 -fno-inline -Wall" make -B x; ./x; nm -C x | egrep '&&' | cut -d' ' -f3- | |
g++ -std=gnu++11 -O3 -fno-inline -Wall x.cc -o x | |
ImplicitDelete is actually copying | |
ImplicitDelete is actually copying | |
Implicit is moving | |
Explicit::Explicit(Explicit&&) | |
Implicit::Implicit(Implicit&&) | |
std::remove_reference<Implicit&>::type&& std::move<Implicit&>(Implicit&) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that
ImplicitDelete
's move ctor cannot be trivial, as its memberexp
has a non-trivial move ctor. A trivial move ctor would not appear in the symbol output.