C ++ enable

编程入门 行业动态 更新时间:2024-10-18 18:15:35
C ++ enable_if可以有默认实现吗?(Can C++ enable_if have a default implementation?)

我想编写一个函数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.

更多推荐

本文发布于:2023-04-28 03:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329716.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:enable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!