-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
## 返回值无法确定问题 | ||
|
||
- 通过模版偏特化来确定返回值 | ||
- 通过后置返回值类型来解决 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <iostream> | ||
|
||
template <typename _tp1, typename _tp2> | ||
struct _return_type { | ||
typedef double value_type; | ||
}; | ||
|
||
template <typename _tp1> | ||
struct _return_type<_tp1, double> { | ||
typedef double value_type; | ||
}; | ||
|
||
template <typename _tp2> | ||
struct _return_type<double, _tp2> { | ||
typedef double value_type; | ||
}; | ||
|
||
template <> | ||
struct _return_type<int, int> { | ||
typedef int value_type; | ||
}; | ||
|
||
template <> | ||
struct _return_type<int, float> { | ||
typedef float value_type; | ||
}; | ||
|
||
template <> | ||
struct _return_type<float, int> { | ||
typedef float value_type; | ||
}; | ||
|
||
template <> | ||
struct _return_type<float, float> { | ||
typedef float value_type; | ||
}; | ||
|
||
|
||
template <typename _tp1, typename _tp2> | ||
inline typename _return_type<_tp1, _tp2>::value_type | ||
max(const _tp1& __a, const _tp2& __b) { | ||
return __a > __b ? __a : __b; | ||
} | ||
|
||
int main() { | ||
std::cout << max(3, 4.5) << std::endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include <iostream> | ||
|
||
template<typename T, typename U> | ||
auto max(const T& __t, const U& __u) -> decltype(__t + __u) { | ||
return __t > __u ? __t : __u; | ||
} | ||
|
||
auto main() -> int { | ||
std::cout << max(3, 4.5) << std::endl; | ||
return 0; | ||
} |