Skip to content

Instantly share code, notes, and snippets.

@m5knt
Created March 21, 2017 08:10
Show Gist options
  • Save m5knt/49fe7fa398cf57f9b54ca3bac6a40dcd to your computer and use it in GitHub Desktop.
Save m5knt/49fe7fa398cf57f9b54ca3bac6a40dcd to your computer and use it in GitHub Desktop.
C++のコンテナ挙動確認用クラス
struct Foo;
std::ostream& operator<<(std::ostream& out, const Foo& self);
struct Foo : std::string {
typedef std::string Base;
~Foo() {
std::cout << "dtor : " <<
std::addressof(*this) <<
" " << *this << std::endl;
}
Foo() : std::string() {
std::cout << "default cstr : "
<< std::addressof(*this) <<
" " << *this << std::endl;
}
Foo(const std::string& str) : std::string(str) {
std::cout << "ctor str : " <<
std::addressof(*this) <<
" " << *this << std::endl;
}
Foo(const Foo& other) : std::string(other) {
std::cout << "copy ctor : " <<
"from " << std::addressof(other) <<
" to " << std::addressof(*this) <<
" " << *this << std::endl;
}
Foo(Foo&& other) : std::string(std::move(other)) {
std::cout << "move ctor : " <<
"from " << std::addressof(other) <<
" to " << std::addressof(*this) <<
" " << *this << std::endl;
}
auto operator=(const Foo& other) -> Foo& {
std::string::operator=(other);
std::cout << "copy : " <<
"from " << std::addressof(other) <<
" to " << std::addressof(*this) <<
" " << *this << std::endl;
return *this;
}
auto operator=(Foo&& other) -> Foo& {
std::string::operator=(std::move(other));
std::cout << "copy : " <<
"from " << std::addressof(other) <<
" to " << std::addressof(*this) <<
" " << *this << std::endl;
return *this;
}
};
std::ostream& operator<<(std::ostream& out, const Foo& self) {
return out << self.c_str();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment