Skip to content

fix compile error #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions code/3/3.1.lambda.basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <iostream>
#include <utility>
#include <memory>

void lambda_value_capture() {
int value = 1;
Expand Down
2 changes: 1 addition & 1 deletion code/3/3.3.rvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void reference(std::string&& str) {
int main()
{
std::string lv1 = "string,"; // lv1 is a lvalue
// std::string&& r1 = s1; // illegal, rvalue can't ref to lvalue
// std::string&& r1 = lv1; // illegal, rvalue can't ref to lvalue
std::string&& rv1 = std::move(lv1); // legal, std::move can convert lvalue to rvalue
std::cout << rv1 << std::endl; // string,

Expand Down
8 changes: 4 additions & 4 deletions code/3/3.5.move.semantics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ class A {
A():pointer(new int(1)) {
std::cout << "construct" << pointer << std::endl;
}
A(A& a):pointer(new int(*a.pointer)) {
A(const A& a):pointer(new int(*a.pointer)) {
std::cout << "copy" << pointer << std::endl;
} // meaningless object copy
A(A&& a):pointer(a.pointer) {
A(A&& a):pointer(a.pointer) {
a.pointer = nullptr;
std::cout << "move" << pointer << std::endl;
}
~A(){
~A() {
std::cout << "destruct" << pointer << std::endl;
delete pointer;
}
};
// avoid compiler optimization
// avoid compiler RVO optimization
A return_rvalue(bool test) {
A a,b;
if(test) return a; // equal to static_cast<A&&>(a);
Expand Down
1 change: 1 addition & 0 deletions code/4/4.1.linear.container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <iostream>
#include <array>
#include <vector>
#include <algorithm>

void foo(int *p, int len) {
for (int i = 0; i != len; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion code/5/5.1.shared.ptr.a.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int main()
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; // reference count equals 0, illegal access
// std::cout << *pointer3 << std::endl; // reference count equals 0, illegal access


// Before leaving the scope, the pointer is destructed and
Expand Down
7 changes: 3 additions & 4 deletions code/5/5.2.unique.ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ void f(const Foo &) {

int main() {
std::unique_ptr<Foo> p1(std::make_unique<Foo>());

// p1 is not empty, prints
if (p1) p1->foo();
{
Expand All @@ -32,15 +31,15 @@ int main() {
f(*p2);

// p2 is not empty, prints
if(p2) p2->foo();
if (p2) p2->foo();

// p1 is empty, no prints
if(p1) p1->foo();
if (p1) p1->foo();

p1 = std::move(p2);

// p2 is empty, no prints
if(p2) p2->foo();
if (p2) p2->foo();
std::cout << "p2 was destroied" << std::endl;
}
// p1 is not empty, prints
Expand Down
1 change: 1 addition & 0 deletions code/5/5.3.weak.ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class A {
class B {
public:
std::shared_ptr<A> pointer;
// std::weak_ptr<A> pointer;
~B() {
std::cout << "B was destroied" << std::endl;
}
Expand Down
4 changes: 3 additions & 1 deletion code/7/7.2.critical.section.a.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <iostream>
#include <thread>
#include <mutex>

int v = 1;

Expand All @@ -23,7 +24,8 @@ void critical_section(int change_v) {
}

int main() {
std::thread t1(critical_section, 2), t2(critical_section, 3);
std::thread t1(critical_section, 2);
std::thread t2(critical_section, 3);
t1.join();
t2.join();

Expand Down
1 change: 1 addition & 0 deletions code/7/7.3.critical.section.b.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <iostream>
#include <thread>
#include <mutex>

int v = 1;

Expand Down
2 changes: 1 addition & 1 deletion code/7/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
all: $(patsubst %.cpp, %.out, $(wildcard *.cpp))

%.out: %.cpp Makefile
clang++ $< -o $@ -std=c++2a -pedantic
clang++ $< -o $@ -std=c++2a -pedantic -pthread -latomic

clean:
rm *.out