Skip to content

Instantly share code, notes, and snippets.

@Yang-33
Last active October 7, 2019 15:56
Show Gist options
  • Save Yang-33/62f811fa5e6a7e0cc2f39ff8071e66c7 to your computer and use it in GitHub Desktop.
Save Yang-33/62f811fa5e6a7e0cc2f39ff8071e66c7 to your computer and use it in GitHub Desktop.
インターンで登場したやつ
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,e) for(int i = a;i < e;++i)
using LL = long long;
// Put this in the declarations for a class to be uncopyable.
#define DISALLOW_COPY(TypeName) \
TypeName(const TypeName&) = delete
// Put this in the declarations for a class to be unassignable.
#define DISALLOW_ASSIGN(TypeName) TypeName& operator=(const TypeName&) = delete
// Put this in the declarations for a class to be uncopyable and unassignable.
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
DISALLOW_COPY(TypeName); \
DISALLOW_ASSIGN(TypeName)
struct B {
int b_;
B(int b) :b_(b) {
puts("B called");
}
//B(B&&x) noexcept :b_(move(x.b_)) {}
DISALLOW_COPY_AND_ASSIGN(B);
};
struct A {
string s;
B* dis_copy;
A(const A& x) {
s = x.s;
dis_copy = nullptr;
puts("A const& called");
}
A(A&& x)noexcept :
s(move(x.s)),
dis_copy(move(x.dis_copy)) {
puts("A move called");
}
A() { puts("A () called"); }
void setB(B* x) {
dis_copy = x;
}
void sets(string ss) {
s = ss;
}
};
// こんな感じ: Aの中にコピー&代入禁止のBがある.
// Aをmoveし続けてもってきたが,
// 1つ以外はコピー時にBをnullptrでコピーしたい
// 1つに限ってはBをmoveしておきたい.
void f(const A& a) {
puts("f");
cout << a.s << endl;
if (a.dis_copy) {
cout << a.dis_copy->b_ << endl;
}
else {
puts("not pointer");
}
puts("");
}
void f2(A a) {
puts("f2");
cout << a.s << endl;
if (a.dis_copy!=nullptr) {
cout << a.dis_copy->b_ << endl;
}
else {
puts("not pointer");
}
puts("");
}
void foo(A mori) {
A kibi = mori; // const&
puts("--");
f(kibi);
f(mori);
f2(kibi);
f2(move(mori)); // こんな感じのはず
f2(kibi);
}
int main() {
A mori; // A()
{
B* uku = new B(1);
mori.sets("aa");
mori.setB(uku);
}
foo(move(mori));
}
@Yang-33
Copy link
Author

Yang-33 commented Oct 7, 2019

結果は

A () called
B called
A move called
A const& called
--
f
aa
not pointer

f
aa
1

A const& called
f2
aa
not pointer

A move called
f2
aa
1

A const& called
f2
aa
not pointer

となる

@Yang-33
Copy link
Author

Yang-33 commented Oct 7, 2019

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,e) for(int i = a;i < e;++i)
using LL = long long;

// Put this in the declarations for a class to be uncopyable.
#define DISALLOW_COPY(TypeName) \
  TypeName(const TypeName&) = delete
// Put this in the declarations for a class to be unassignable.
#define DISALLOW_ASSIGN(TypeName) TypeName& operator=(const TypeName&) = delete
// Put this in the declarations for a class to be uncopyable and unassignable.
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
  DISALLOW_COPY(TypeName);                 \
  DISALLOW_ASSIGN(TypeName)


struct B {
	int b_;
	B(int b) :b_(b) {
		puts("B called");
	}
	//B(B&&x) noexcept :b_(move(x.b_)) {}
	DISALLOW_COPY_AND_ASSIGN(B);

};

struct A {
	string s;
	B* dis_copy;
	A(const A& x) {
		s = x.s;
		dis_copy = nullptr;
		puts("A const& called");
	}
	A& operator = (const A& x) {
		// かきかえてもOK

		return *this;
	}
	//A operator = (A) {

	//}
	A& operator = (A) {

	}
	A(A&& x)noexcept :
		s(move(x.s)),
		dis_copy(move(x.dis_copy)) {
		puts("A move called");
	}


	// ムーブ代入演算子は?
	A& operator = (A&& v) {
		puts("!!!!");
		// こっちは = のoperatorを定義していた
		// 要検討だね
		if (&v != this) { // 自己代入の回避
			delete dis_copy;
			dis_copy = v.dis_copy;
			v.dis_copy = nullptr;

		}
		return *this;
	}
	A() { puts("A () called"); }

	void setB(B* x) {
		dis_copy = x;
	}
	void sets(string ss) {
		s = ss;
	}

};

// こんな感じ: Aの中にコピー&代入禁止のBがある.
// Aをmoveし続けてもってきたが,
// 1つ以外はコピー時にBをnullptrでコピーしたい
// 1つに限ってはBをmoveしておきたい.

void f(const A& a) {
	puts("f");
	cout << a.s << endl;
	if (a.dis_copy) {
		cout << a.dis_copy->b_ << endl;
	}
	else {
		puts("not pointer");
	}
	puts("");
}

void f2(A a) {
	puts("f2");
	cout << a.s << endl;
	if (a.dis_copy != nullptr) {
		cout << a.dis_copy->b_ << endl;
	}
	else {
		puts("not pointer");
	}
	puts("");
}

void foo(A mori) {
	A kibi = mori; // const&
	puts("--");
	f(kibi);
	f(mori);

	f2(kibi);
	f2(move(mori)); // こんな感じのはず
	f2(kibi);
}
int main() {

	A mori; // A()
	{
		B* uku = new B(1);
		mori.sets("aa");
		mori.setB(uku);
	}
	foo(move(mori));
}

@Yang-33
Copy link
Author

Yang-33 commented Oct 7, 2019

わかった

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