-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtype_deduction.cpp
More file actions
37 lines (29 loc) · 938 Bytes
/
type_deduction.cpp
File metadata and controls
37 lines (29 loc) · 938 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
//*****************************************************************************
//
// Author: Michael Price
// License: Attribution-NonCommercial-NoDerivs 3.0 Unported
// http://creativecommons.org/licenses/by-nc-nd/3.0/legalcode
//
//*****************************************************************************
#ifndef WIN32
namespace std {
class string { public: string(const char *) { } string operator+(string other) { return other; } };
}
#endif // WIN32
template <typename T, typename U>
auto some_calculation (T t, U u) -> decltype(t+u)
{
return t + u;
}
void deduce_types ()
{
// 'i' is an int
auto i = 1;
// 'j' is an int
decltype(i) j = i + 1;
// 'x' is whatever 'int+int' is (an int)
auto x = some_calculation(41, 1);
// 'y' is whatever 'string+string' is (a string)
auto y = some_calculation(std::string("Hello"),
std::string("World"));
}