Skip to content

Commit

Permalink
2021-04-28 09:33:23 CST W17D3
Browse files Browse the repository at this point in the history
  • Loading branch information
aggresss committed Apr 28, 2021
1 parent 835ef04 commit 43f3365
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions metaprogramming/return_type/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 返回值无法确定问题

- 通过模版偏特化来确定返回值
- 通过后置返回值类型来解决
48 changes: 48 additions & 0 deletions metaprogramming/return_type/hello1.cpp
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;
}
11 changes: 11 additions & 0 deletions metaprogramming/return_type/hello2.cpp
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;
}

0 comments on commit 43f3365

Please sign in to comment.