I am given the below "interface" to implement:
enum class minmax_t : uint8_t { MIN, MAX };
template <minmax_t M, typename T>
constexpr T get(const std::vector<T> &v1, const std::vector<T> &v2, const std::vector<T> &v3);
so that the following driver function should return the min/max value from three vectors:
auto max = get<minmax_t::MIN, double>(v0, v1, v2);
auto min = get<minmax_t::MAX, int>(v0, v1, v2);
where v0, v1 and v2 are std::vectors of elements that can be compared. My implementation is below:
template <minmax_t M, typename T>
constexpr T get(const std::vector<T> &v1, const std::vector<T> &v2,
const std::vector<T> &v3) {
if constexpr (M == minmax_t::MAX) {
auto v1m = *std::max_element(v1.begin(), v1.end());
auto v2m = *std::max_element(v2.begin(), v2.end());
auto v3m = *std::max_element(v3.begin(), v3.end());
if (v1m > v2m && v1m > v3m)
return v1m;
if (v2m > v1m && v2m > v3m)
return v2m;
return v3m;
} else {
auto v1m = *std::min_element(v1.begin(), v1.end());
auto v2m = *std::min_element(v2.begin(), v2.end());
auto v3m = *std::min_element(v3.begin(), v3.end());
if (v1m < v2m && v1m < v3m)
return v1m;
if (v2m < v1m && v2m < v3m)
return v2m;
return v3m;
}
}
It worked, but given the vastness of metaprogramming and STL, it is very likely that I missed some gems from C++ to make the implementation better (e.g., more expressive/concise/performant). Any thoughts?
concat/joinis correct (in regard to the bug mentioned above), because it doesn't require min or max to exist for individual vectors. \$\endgroup\$