Skip to content

Commit e7d607a

Browse files
Merge branch 'master' of https://github.com/chronolaw/cpp_study
2 parents 863994d + d7df439 commit e7d607a

3 files changed

Lines changed: 67 additions & 7 deletions

File tree

section1/compile.cpp

Lines changed: 34 additions & 0 deletions
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>
@@ -33,6 +36,22 @@ struct fib<1>
3336
static const int value = 1;
3437
};
3538

39+
#if __cplusplus >= 201402
40+
constexpr int const_fib(int n)
41+
{
42+
if (n <= 1) {
43+
return 1;
44+
}
45+
46+
return const_fib(n - 1) + const_fib(n - 2);
47+
}
48+
#else // C++11
49+
int const_fib(int n)
50+
{
51+
return 42;
52+
}
53+
#endif
54+
3655
//[[deprecated("deadline:2020-12-31")]] // c++14 or later
3756
[[gnu::deprecated]] // c+11 or later
3857
int old_func()
@@ -107,6 +126,18 @@ void check_type(T v)
107126
cout << is_void<void>::value << endl;
108127
}
109128

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+
110141
int main()
111142
{
112143
using namespace std;
@@ -116,13 +147,16 @@ int main()
116147
cout << fib<4>::value << endl;
117148
cout << fib<5>::value << endl;
118149

150+
cout << const_fib(10) << endl;
151+
119152
old_func();
120153
get_num();
121154

122155
case2();
123156
case3();
124157

125158
case4();
159+
case5();
126160

127161
check_type(10);
128162
//check_type((void*)0);

section4/sample_v3.proto

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2020 by Chrono
2+
//
3+
// protoc --cpp_out=. sample_v3.proto
4+
5+
syntax = "proto3";
6+
7+
package sample;
8+
9+
message Vendor
10+
{
11+
uint32 id = 1;
12+
string name = 2;
13+
bool valid = 3;
14+
string tel = 4;
15+
}
16+
17+
message Product
18+
{
19+
int32 id = 1;
20+
string name = 2;
21+
22+
string tag = 3;
23+
24+
Vendor vendor = 10;
25+
}

section5/srv.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ try
7373
// async process msg
7474

7575
// todo: try-catch
76-
std::async(std::launch::async,
76+
//auto f = std::async(std::launch::async,
77+
std::thread(
7778
[&sum, msg_ptr]()
7879
//[&sum, &count](decltype(msg_ptr) ptr)
7980
{
@@ -90,7 +91,7 @@ try
9091
//debug_print(book);
9192

9293
sum.add_sales(book);
93-
}); // async
94+
}).detach(); // async
9495
} // for(;;)
9596
}; // recv_cycle lambda
9697

@@ -126,13 +127,13 @@ try
126127
} // for(;;)
127128
}; // log_cycle lambda
128129

129-
// launch recv_cycle then wait
130-
auto fu = std::async(std::launch::async, recv_cycle);
131-
132130
// launch log_cycle
133-
std::async(std::launch::async, log_cycle);
131+
auto fu1 = std::async(std::launch::async, log_cycle);
132+
133+
// launch recv_cycle then wait
134+
auto fu2 = std::async(std::launch::async, recv_cycle);
134135

135-
fu.wait();
136+
fu2.wait();
136137
}
137138
catch(std::exception& e)
138139
{

0 commit comments

Comments
 (0)