-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoercion_ctor.cpp
More file actions
49 lines (37 loc) · 1006 Bytes
/
coercion_ctor.cpp
File metadata and controls
49 lines (37 loc) · 1006 Bytes
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
#include <iostream>
#include <memory>
#include <type_traits>
struct base{};
struct derived: base{};
template <class T>
class Ptr {
public:
Ptr() = default;
Ptr(const Ptr& other): ptr(other.ptr){
std::cout << "cctor" << std::endl;
}
template <class U>
Ptr(Ptr<U> const& other): ptr(other.ptr) {
static_assert(std::is_base_of<T,U>::value, "convertion is not possible");
std::cout << "coercing member template cctor" << std::endl;
}
Ptr& operator=(const Ptr& other) {
ptr = other.ptr;
std::cout << "ccop" << std::endl;
return *this;
}
template <class U>
Ptr& operator=(Ptr<U> const& other) {
static_assert(std::is_base_of<T,U>::value, "convertion is not possible");
ptr = other.ptr;
std::cout << "other ccop" << std::endl;
return *this;
}
T* ptr;
};
int main() {
std::unique_ptr<base> ptr{new derived{}};
Ptr<derived> p1;
Ptr<base> b1 = p1;
return 0;
}