-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforward-implicit-conv.cc
More file actions
62 lines (51 loc) · 867 Bytes
/
forward-implicit-conv.cc
File metadata and controls
62 lines (51 loc) · 867 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
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <type_traits>
#include <iostream>
int f(const int&)
{
return 1;
}
int f(int&&)
{
return 2;
}
int f(const double&)
{
return 3;
}
int f(double&&)
{
return 4;
}
// have this g equivalent to the h below?
template <class U>
std::enable_if_t<std::is_convertible<std::decay_t<U>, int>::value, int>
g(U&& x)
{
using target_type = std::conditional_t<std::is_same<std::decay_t<U>, int>::value, U&&, int&&>;
return f(static_cast<target_type>(x));
}
int h(const int& x)
{
return f(x);
}
int h(int&& x)
{
return f((int&&)x);
}
int main()
{
int lint = 1;
double ldouble = 3;
// all are 2, but I want the first to be 1
std::cout
<< h(lint) << '\n'
<< h(2) << '\n'
<< h(ldouble) << '\n'
<< h(4.0) << '\n'
<< '\n'
<< g(lint) << '\n'
<< g(2) << '\n'
<< g(ldouble) << '\n'
<< g(4.0) << '\n';
return 0;
}