-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnullptr.cpp
More file actions
69 lines (54 loc) · 1.01 KB
/
nullptr.cpp
File metadata and controls
69 lines (54 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <mutex>
#include <memory>
void f(int i)
{
std::cout << "call f(int)\n";
}
void f(bool b)
{
std::cout << "call f(bool)\n";
}
void f(void * ptr)
{
std::cout << "call f(void *)\n";
}
class widget {
};
int f1(std::shared_ptr<widget> spw)
{
std::cout << "call f1(std::shared_ptr<widget> spw))\n";
return 0;
}
double f2(std::unique_ptr<widget> upw)
{
std::cout << "call f2(std::unique_ptr<widget> upw))\n";
return 1.0;
}
bool f3(widget* pw)
{
std::cout << "call f3(widget* pw)\n";
return false;
}
template<typename FuncType,
typename MuxType,
typename PtrType>
decltype(auto) lockAndCall(FuncType func,
MuxType& mutex,
PtrType ptr)
{
using MuxGuard = std::lock_guard<MuxType>;
MuxGuard g(mutex);
return func(ptr);
}
int main()
{
std::mutex f1m, f2m, f3m;
auto result1 = lockAndCall(f1, f1m, 0);//error
auto result2 = lockAndCall(f2, f2m, NULL);//error
auto result3 = lockAndCall(f3, f3m, nullptr);
f(0);
//f(NULL);// error
f(nullptr);
return 0;
}