-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP095.SpecialMemeberFunctionTemplates.cpp
More file actions
64 lines (60 loc) · 1.53 KB
/
Copy pathP095.SpecialMemeberFunctionTemplates.cpp
File metadata and controls
64 lines (60 loc) · 1.53 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
#include <iostream>
#include <string>
#include <type_traits>
class Person
{
private:
std::string name;
public:
Person(const std::string& _name) : name(_name) // 1
{
std::cout << "Person(const std::string& _name)" << std::endl;
}
Person(std::string&& _name) : name(std::move(_name)) // 2
{
std::cout << "Person(std::string&& _name)" << std::endl;
}
Person(const Person& other) : name(other.name) // 3
{
std::cout << "Person(const Person& other)" << std::endl;
}
Person(Person&& other) : name(std::move(other.name)) // 4
{
std::cout << "Person(Person&& other)" << std::endl;
}
};
// make it generic
class Person2
{
private:
std::string name;
public:
// SFINAE
template<typename T,
typename = std::enable_if_t<std::is_convertible_v<T, std::string>>>
explicit Person2(T&& _name) : name(std::forward<T>(_name)) // 1
{
std::cout << "template<typename T> Person2(T&& _name)" << std::endl;
}
Person2(const Person2& other) : name(other.name) // 2
{
std::cout << "Person2(const Person2& other)" << std::endl;
}
Person2(Person2&& other) : name(std::move(other.name)) // 3
{
std::cout << "Person2(Person2&& other)" << std::endl;
}
};
int main(int argc, char const *argv[])
{
std::string name = "kim";
Person p1(name);
Person p2("kim");
Person p3(p2);
Person p4(std::move(p2));
Person2 p21(name);
Person2 p22("kim");
Person2 p23(p22);
Person2 p24(std::move(p22));
return 0;
}