Skip to content

Instantly share code, notes, and snippets.

@Irfy
Created November 30, 2015 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Irfy/666c68d0d4c973a12bdf to your computer and use it in GitHub Desktop.
Save Irfy/666c68d0d4c973a12bdf to your computer and use it in GitHub Desktop.
Demoing defaulted move ctor neither being generated nor called (see ImplicitDelete)
#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&)
@Irfy
Copy link
Author

Irfy commented Nov 30, 2015

Note that ImplicitDelete's move ctor cannot be trivial, as its member exp has a non-trivial move ctor. A trivial move ctor would not appear in the symbol output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment