可变模板函数的参数包的筛选
Filtering of a parameter pack of the variadic template function
我想像下面的代码一样"过滤"可变模板函数的参数包(只需要"过滤"某些类型的变量):
#include <iostream>
#include <utility>
#include <cstdlib>
struct Z {};
struct test
{
using result_type = void;
template< typename... P >
result_type apply_filter(P &&... _p) const
{
using std::forward;
return operator () (forward< P >(_p)...);
}
template< typename... T >
result_type operator () (std::string const & _s, T &&... _tail) const
{
std::cout << _s << std::endl;
return operator () (std::forward< T >(_tail)...);
}
template< typename... T >
result_type operator () (double const & _x, T &&... _tail) const
{
std::cout << _x << std::endl;
return operator () (std::forward< T >(_tail)...);
}
template< typename... T >
result_type operator () (Z const &, T &&... _tail) const
{
std::cout << "z" << std::endl;
return operator () (std::forward< T >(_tail)...);
}
private :
result_type operator () () const { return; }
template< typename T, typename U >
using is_the_same = std::is_same< typename std::remove_const< typename std::remove_reference< T >::type >::type, U >;
template< typename T >
typename std::enable_if< is_the_same< T, std::string >::value, std::string >::type
forward(T && _s) const
{
return """ + _s + """;
}
template< typename T >
typename std::enable_if< is_the_same< T, double >::value, double >::type
forward(T && _x) const
{
return _x + 1.0;
}
};
int main()
{
test test_;
double x = 0.0;
std::string s = "s";
Z z;
test_.apply_filter(x, s, z);
return EXIT_SUCCESS;
}
但是CCD_ 1的优先级高于CCD_ 2中的成员函数。因此,这里没有发生过滤。
有什么变通办法吗?
这应该可以做到:
#include <iostream>
#include <utility>
struct Z { };
struct test
{
using result_type = void;
template< typename... P >
result_type apply_filter(P &&... _p) const
{
return operator () (forward<P>(_p)...);
}
template< typename... T >
result_type operator () (std::string const & _s, T &&... _tail) const
{
std::cout << _s << std::endl;
return operator () (std::forward< T >(_tail)...);
}
template< typename... T >
result_type operator () (double const & _x, T &&... _tail) const
{
std::cout << _x << std::endl;
return operator () (std::forward< T >(_tail)...);
}
template< typename... T >
result_type operator () (Z const &, T &&... _tail) const
{
std::cout << "z" << std::endl;
return operator () (std::forward< T >(_tail)...);
}
private:
result_type operator () () const { }
template< typename T, typename U >
using is_the_same = std::is_same<typename std::remove_const<typename std::remove_reference<T>::type>::type, U>;
template<typename T>
using is_known_type = typename std::conditional<(is_the_same<T, std::string>::value || is_the_same<T, double>::value), std::true_type, std::false_type>::type;
template<typename T>
typename std::enable_if<is_the_same<T, std::string>::value, std::string>::type
forward_impl(T && _s, std::true_type) const
{
return """ + _s + """;
}
template<typename T>
typename std::enable_if<is_the_same<T, double>::value, double>::type
forward_impl(T && _x, std::true_type) const
{
return _x + 1.0;
}
#define RETURNS(exp) -> decltype(exp) { return exp; }
template<typename T>
auto forward_impl(T && t, std::false_type) const
RETURNS(std::forward<T>(t))
template<typename T>
auto forward(T && t) const
RETURNS(forward_impl(std::forward<T>(t), is_known_type<T>()))
#undef RETURNS
};
int main(int argc, char ** argv) {
test test_;
double x = 0.0;
std::string s = "s";
Z z;
test_.apply_filter(x, s, z);
return 0;
}
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 使用不带参数的函数访问结构元素
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 如何使用Luacneneneba API正确读取字符串和表参数
- 参数包中的筛选器类型
- C++可变参数模板类型筛选转换
- 筛选参数包的类型
- 可变模板函数的参数包的筛选
- 模板非类型参数常量限制筛选器库
- 使自定义Directshow筛选器接受参数