Skip to content

Commit f231e74

Browse files
committed
Merge branch 'master' of github.com:chronolaw/cpp_study
2 parents d747672 + 1a49825 commit f231e74

File tree

10 files changed

+132
-1
lines changed

10 files changed

+132
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@
3535
a.out
3636
*.swp
3737
*.pb.*
38+
example_*_cpp*

Makefile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
sub_dirs=$(wildcard section[0-3])
2+
3+
sub_builds=$(addprefix build_,$(sub_dirs))
4+
sub_cleans=$(addprefix clean_,$(sub_dirs))
5+
6+
all: $(sub_builds)
7+
8+
clean: $(sub_cleans)
9+
10+
build_%: %
11+
make -C $^
12+
13+
clean_%: %
14+
make -C $^ clean

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# cpp_study
22

3-
《C++实践经验谈》(delayed to May) Follow me to study modern C++.
3+
[《C++实战笔记》](https://time.geekbang.org/column/intro/309) Follow me to study modern C++.
44

55
Pull requests of make/cmake are welcome!
66

common.mk

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
CXX_FLAGS=-Wall
2+
CXX=g++
3+
4+
HAS_CPP14=$(shell \
5+
echo > /tmp/dummy.cpp ; \
6+
if $(CXX) -std=c++14 -c -o /dev/null /tmp/dummy.cpp 2>/dev/null ; then \
7+
echo yes ; \
8+
else \
9+
echo no ; \
10+
fi)
11+
12+
HAS_CPP11=$(shell \
13+
echo > /tmp/dummy.cpp ; \
14+
if $(CXX) -std=c++11 -c -o /dev/null /tmp/dummy.cpp 2>/dev/null ; then \
15+
echo yes ; \
16+
else \
17+
echo no ; \
18+
fi)
19+
20+
ifeq ($(HAS_CPP14), yes)
21+
PREFER_STANDARD:=cpp14
22+
else
23+
ifeq ($(HAS_CPP11), yes)
24+
PREFER_STANDARD:=cpp11
25+
endif
26+
endif
27+
28+
all: examples
29+
30+
example_%_cpp14: %.cpp
31+
$(CXX) -std=c++14 -o $@ $^ $(CXX_FLAGS) $($@_cxx_flags) $($@_ld_flags)
32+
33+
example_%_cpp11: %.cpp
34+
$(CXX) -std=c++11 -o $@ $^ $(CXX_FLAGS) $($@_cxx_flags) $($@_ld_flags)
35+
36+
example_%_cpp98: %.cpp
37+
$(CXX) -std=c++98 -o $@ $^ $(CXX_FLAGS) $($@_cxx_flags) $($@_ld_flags)
38+
39+
clean:
40+
rm -fr example_*

section0/Makefile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
include ../common.mk
2+
3+
ifeq ($(HAS_CPP14), yes)
4+
examples: example_test_cpp14
5+
endif
6+
7+
ifeq ($(HAS_CPP11), yes)
8+
examples: example_test_cpp11
9+
endif

section1/Makefile

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
include ../common.mk
2+
3+
4+
ifeq ($(HAS_CPP14), yes)
5+
examples: \
6+
example_codestyle_cpp14 \
7+
example_compile_cpp14 \
8+
example_oop_cpp14 \
9+
example_preprocess_cpp14
10+
endif
11+
12+
ifeq ($(HAS_CPP11), yes)
13+
examples: \
14+
example_codestyle_cpp11 \
15+
example_compile_cpp11 \
16+
example_oop_cpp11 \
17+
example_preprocess_cpp11
18+
endif
19+
20+
# example_preprocess_cpp98 is showing version check with compile failed.
21+
# please explicit using `make example_preprocess_cpp98` under section1 folder

section1/compile.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
//
88
// gcc -E -dM - < /dev/null
99

10+
#include <cassert>
11+
1012
#include <typeinfo>
13+
#include <string>
1114
#include <iostream>
1215
#include <stdexcept>
1316
#include <type_traits>
@@ -123,6 +126,18 @@ void check_type(T v)
123126
cout << is_void<void>::value << endl;
124127
}
125128

129+
void case5()
130+
{
131+
int i = 10;
132+
int *p = &i;
133+
134+
assert(i > 0 && "i must be greater than zero");
135+
assert(p != nullptr);
136+
137+
std::string str = "hello";
138+
assert(!str.empty());
139+
}
140+
126141
int main()
127142
{
128143
using namespace std;
@@ -141,6 +156,7 @@ int main()
141156
case3();
142157

143158
case4();
159+
case5();
144160

145161
check_type(10);
146162
//check_type((void*)0);

section1/preprocess.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ int main()
122122
case2();
123123

124124
my_own::case3();
125+
126+
[[gnu::unused]] // ignore warning
125127
my_own::MyClass obj;
126128

127129
case4();

section2/Makefile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
include ../common.mk
2+
3+
ifeq ($(HAS_CPP14), yes)
4+
examples: \
5+
example_auto_cpp14 \
6+
example_const_cpp14 \
7+
example_exception_cpp14 \
8+
example_lambda_cpp14 \
9+
example_smart_ptr_cpp14
10+
endif
11+
12+
ifeq ($(HAS_CPP11), yes)
13+
examples: \
14+
example_exception_cpp11
15+
endif

section3/Makefile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
include ../common.mk
2+
3+
example_thread_cpp14_ld_flags=-lpthread
4+
example_thread_cpp11_ld_flags=-lpthread
5+
6+
7+
ifeq ($(HAS_CPP14), yes)
8+
examples: \
9+
example_algo_cpp14 \
10+
example_container_cpp14 \
11+
example_string_cpp14 \
12+
example_thread_cpp14
13+
endif

0 commit comments

Comments
 (0)