我想编写一个函数print ,根据其参数的类型,它的行为会有所不同。
这是我的实施:
template <typename T, typename std::enable_if<std::is_array<T>::value, int>::type = 0> void print(const T &v) { std::cout << "array: "; for (const auto &e : v) { std::cout << e << ", "; } std::cout << std::endl; } template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0> void print(const T &v) { std::cout << "integral: " << v << std::endl; } template <typename T, typename std::enable_if<!(std::is_array<T>::value || std::is_integral<T>::value), int>::type = 0> void print(const T &v) { std::cout << "default: " << v << std::endl; }此代码按预期工作,但最后一个规范中的条件过于复杂。
有没有解决方案来简化最后一个?
I want to write a function print which behaves differently according to the type of its argument.
Here is my implementation:
template <typename T, typename std::enable_if<std::is_array<T>::value, int>::type = 0> void print(const T &v) { std::cout << "array: "; for (const auto &e : v) { std::cout << e << ", "; } std::cout << std::endl; } template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0> void print(const T &v) { std::cout << "integral: " << v << std::endl; } template <typename T, typename std::enable_if<!(std::is_array<T>::value || std::is_integral<T>::value), int>::type = 0> void print(const T &v) { std::cout << "default: " << v << std::endl; }This code works as expected, but the conditions in the last specification are too complicated.
Is there any solution to simplify the last one?
最满意答案
可以用于默认情况的一般方法是使用一个带有变量参数列表的函数。 只有在没有其他功能匹配时才会使用此选项。 这是一个例子:
template <typename T, typename std::enable_if<std::is_array<T>::value, int>::type = 0> void print_helper(const T &v,int) { std::cout << "array: "; for (const auto &e : v) { std::cout << e << ", "; } std::cout << std::endl; } template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0> void print_helper(const T &v,int) { std::cout << "integral: " << v << std::endl; } template <typename T> void print_helper(const T &v,...) { std::cout << "default: " << v << std::endl; } template <typename T> void print(const T &v) { print_helper(v,0); }对于只有两个重载,额外的功能可能不值得,但是当你获得更多的重载时,这个表单确实可以为默认情况付出代价。
A general approach you can use for a default case is to have a function which takes a variable argument list. This will only be used if no other function matches. Here is an example:
template <typename T, typename std::enable_if<std::is_array<T>::value, int>::type = 0> void print_helper(const T &v,int) { std::cout << "array: "; for (const auto &e : v) { std::cout << e << ", "; } std::cout << std::endl; } template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0> void print_helper(const T &v,int) { std::cout << "integral: " << v << std::endl; } template <typename T> void print_helper(const T &v,...) { std::cout << "default: " << v << std::endl; } template <typename T> void print(const T &v) { print_helper(v,0); }For only two overloads, the extra function may not be worth it, but as you get more overloads this form can really pay off for the default case.
更多推荐
发布评论