prepare for c++17

This commit is contained in:
Changkun Ou
2018-03-26 09:08:36 +02:00
parent 8a3eb8f271
commit 71025d8bc6
39 changed files with 28 additions and 11 deletions

66
code/5/5.1.cpp Normal file
View File

@@ -0,0 +1,66 @@
#include <iostream>
#include <memory>
void foo(std::shared_ptr<int> i)
{
(*i)++;
}
//struct Base {
// Base() { std::cout << " Base::Base()\n"; }
// ~Base() { std::cout << " Base::~Base()\n"; }
//};
//
//struct Subclass: public Base {
// Subclass() { std::cout << " Subclass::Subclass()\n"; }
// ~Subclass() { std::cout << " Subclass::~Subclass()\n"; }
//};
//
//void thr(std::shared_ptr<Base> p)
//{
// std::this_thread::sleep_for(std::chrono::seconds(1));
// std::shared_ptr<Base> lp = p; // 即使引用计数增加也是线程安全的
// {
// static std::mutex io_mutex;
// std::lock_guard<std::mutex> lk(io_mutex);
// std::cout << "local pointer in a thread:\n"
// << " lp.get() = " << lp.get()
// << ", lp.use_count() = " << lp.use_count() << '\n';
// }
//}
int main()
{
// auto pointer = new int(10); // 非法, 不允许直接赋值
// 构造了一个 std::shared_ptr
auto pointer = std::make_shared<int>(10);
auto pointer2 = pointer; // 引用计数+1
auto pointer3 = pointer; // 引用计数+1
foo(pointer);
std::cout << *pointer << std::endl; // 11
int *p = pointer.get(); // 这样不会增加引用计数
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl;
std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl;
std::cout << "pointer3.use_count() = " << pointer3.use_count() << std::endl;
pointer2.reset();
std::cout << "reset pointer2:" << std::endl;
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl;
std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl;
std::cout << "pointer3.use_count() = " << pointer3.use_count() << std::endl;
pointer3.reset();
std::cout << "reset pointer3:" << std::endl;
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl;
std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl;
std::cout << "pointer3.use_count() = " << pointer3.use_count() << std::endl;
// std::cout << *pointer << std::endl; // 引用计数为0时, 非法访问
// 离开作用域前pointer 会被析构引用计数减为0, 从而释放内存
return 0;
}

41
code/5/5.2.cpp Normal file
View File

@@ -0,0 +1,41 @@
#include <iostream>
#include <memory>
struct Foo {
Foo() { std::cout << "Foo::Foo" << std::endl; }
~Foo() { std::cout << "Foo::~Foo" << std::endl; }
void foo() { std::cout << "Foo::foo" << std::endl; }
};
void f(const Foo &) {
std::cout << "f(const Foo&)" << std::endl;
}
int main() {
std::unique_ptr<Foo> p1(std::make_unique<Foo>());
// p1 不空, 输出
if (p1) p1->foo();
{
std::unique_ptr<Foo> p2(std::move(p1));
// p2 不空, 输出
f(*p2);
// p2 不空, 输出
if(p2) p2->foo();
// p1 为空, 无输出
if(p1) p1->foo();
p1 = std::move(p2);
// p2 为空, 无输出
if(p2) p2->foo();
std::cout << "p2 被销毁" << std::endl;
}
// p1 不空, 输出
if (p1) p1->foo();
// Foo 的实例会在离开作用域时被销毁
}

28
code/5/5.3.cpp Normal file
View File

@@ -0,0 +1,28 @@
#include <iostream>
#include <memory>
class A;
class B;
class A {
public:
std::shared_ptr<B> pointer;
~A() {
std::cout << "A 被销毁" << std::endl;
}
};
class B {
public:
std::shared_ptr<A> pointer;
~B() {
std::cout << "B 被销毁" << std::endl;
}
};
int main() {
std::shared_ptr<A> a = std::make_shared<A>();
std::shared_ptr<B> b = std::make_shared<B>();
a->pointer = b;
b->pointer = a;
return 0;
}