用于推导容器和initializer_list-s的模板函数
template function that deduces both containers and initializer_list-s
我想写一个助手函数,比如:
template <typename F, typename Range1, typename Range2>
auto helper(const Range1& left, const Range2& right, F&& pred)
{
using namespace std; // for cbegin/cend and ADL
return pred(cbegin(left), cend(left), cbegin(right), cend(right));
}
它适用于集装箱:
std::vector<int> v1 = {1,2,3,4,5,6};
std::vector<int> v2 = {5,4,3,2,1,6};
std::cout << helper(v1, v2, [](const auto&... args){ return std::is_permutation(args...);}) << std::endl;
但未能推导出initializer_list
-s(例(:
std::cout << helper({1,2,3,4,5,6}, {5,4,3,2,1,6}, [](const auto&... args){ return std::is_permutation(args...);}) << std::endl;
有没有一种惯用的方法来重写helper
,以便它同时推导容器和initializer_list
-s?
对于容器和initializer_list
的所有组合,我想不出比重载更好的东西了。
我认为这里的根本问题是,像{ 1, 2, 3 }
这样的支持的init列表只是初始值设定项,而不是std::initializer_list<T>
类型的对象。它可能用于初始化某个给定类型的对象。但它本身并不是任何类型的对象。函数模板参数推导规则中似乎没有任何内容允许您从支持的init-list参数中获取std::initializer_list<T>
,除非您的函数参数一开始就被声明为某种std::initializer_list<T>
。
所以我担心写那些过载将是最简单的解决方案…
以下是我能做的最好的事情:
template<class X>
struct Range {
X* container;
Range(X& x):container(std::addressof(x)) {}
Range(X&& x):container(std::addressof(x)) {} // dangerous, but hey
auto begin() const { using std::begin; return begin(*container); }
auto end() const { using std::end; return end(*container); }
auto cbegin() const { using std::cbegin; return cbegin(*container); }
auto cend() const { using std::cend; return cend(*container); }
};
template<class T>
struct Range<std::initializer_list<T>> {
using X=std::initializer_list<T>;
X container;
Range(X x):container(x) {}
auto begin() const { using std::begin; return begin(container); }
auto end() const { using std::end; return end(container); }
auto cbegin() const { using std::cbegin; return cbegin(container); }
auto cend() const { using std::cend; return cend(container); }
};
template<class T>
Range( std::initializer_list<T> ) -> Range<std::initializer_list< T >>;
template<class C1, class C2>
void foo( Range<C1> r1, Range<C2> c2 ) {}
测试代码:
Range r = {{'a', 'b', 'c'}};
(void)r;
std::vector v = {1,2,3};
foo( Range{{'a','b','c'}}, Range{v} );
您必须手动将参数强制转换为Range
才能在调用站点工作,因为类模板参数推导不适用于函数参数。
我们可能会以不同的方式攻击它。
template <typename F, typename Range1, typename Range2>
auto helper(const Range1& left, const Range2& right, F&& pred)
将以上语法更改为链式调用。
helper(v1)({1,2,3})[pred];
这将2^n爆炸减少为2。对2个过载没有太大帮助,但仍然。。。
template<class...Ts>
struct helper_t {
std::tuple<Ts&&...> containers;
template<class T>
helper_t<T, Ts...> operator()(T&& t)&& {
return { /* append-move containers and t into one tuple */ };
}
template<class T>
helper_t<std::initializer_list<T>, Ts...> operator()(std::initializer_list<T> t)&& {
return { /* append-move containers and t into one tuple */ };
}
template<class F>
decltype(auto) operator[](F&& f)&& {
return std::move(*this).apply_impl(
std::make_index_sequence<sizeof...(Ts)>{},
std::forward<F>(f)
);
}
private:
template<std::size_t...Is, class F>
decltype(auto) apply_impl( std::index_sequence<Is...>, F&& f ) && {
using std::cbegin; using std::cend;
using std::get;
return std::forward<F>(f)(
cbegin( get<Is>(std::move(containers)) ), cend( get<Is>(std::move(containers)) )
);
}
};
static constexpr const helper_t<> helper;
我把追加元组作为练习。
helper( container1 )( {1,2,3} )( container2 )[ some_lambda ];
是语法。
请明确指定您传递的参数是initializer_list,如下所示:-
std::cout << helper(v1, std::initializer_list<int>{5,4,3,2,1,6}, [](const auto&... args){ return std::is_permutation(args...);}) << std::endl;
std::cout << helper(std::initializer_list<int>{1,2,3,4,5,6}, std::initializer_list<int>{5,4,3,2,1,6}, [](const auto&... args){ return std::is_permutation(args...);}) << std::endl;
这是您可以传递initializer_list的方法,否则您需要为容器和initializer_list的所有组合重载
正如Michael Kenzel所解释的,问题是一个有支撑的init列表不是std::intializer_list
。
所以我同意Michael(+1(的观点:我看不出有什么方法可以编写一个单一的模板函数来推导STL容器和(作为std::initilizer_list<T>
(支持的init列表。
但是,如果您可以接受为helper()
函数添加一个helper()
辅助函数,那么您可以使用这样一个事实,即一个有支撑的init列表可以推断为C样式数组。
因此,您可以为C样式数组编写一个特定于helper()
的版本,它可以转换std::initializer_list
(或std::array
,或…见(中的C样式数组,或者,在我的示例中,您可以简单地调用原始的helper()
函数来传递数组,但显示类型。
我的意思是:你可以添加这个helper()
助手功能
template <typename F, typename R1, std::size_t S1,
typename R2, std::size_t S2>
auto helper (R1 const (&r1)[S1], R2 const (&r2)[S2], F && pred)
{ return helper<F, R1[S1], R2[S2]>(r1, r2, std::forward<F>(pred)); }
缺点:如果两个范围都是STL容器,或者如果两个区域都是C样式数组(或支持的init列表(,则此方法有效;如果可以有一个STL容器和一个C样式数组,则必须编写另外两个helper()
辅助函数:一个用于(仅(第一个范围,另一个用于第二个范围。
以下是的完整工作示例
#include <vector>
#include <iostream>
#include <algorithm>
template <typename F, typename Range1, typename Range2>
auto helper (Range1 const & left, Range2 const & right, F && pred)
{ return pred(std::cbegin(left), std::cend(left),
std::cbegin(right), std::cend(right)); }
template <typename F, typename R1, std::size_t S1,
typename R2, std::size_t S2>
auto helper (R1 const (&r1)[S1], R2 const (&r2)[S2], F && pred)
{ return helper<F, R1[S1], R2[S2]>(r1, r2, std::forward<F>(pred)); }
int main ()
{
std::vector<int> v1 = {1,2,3,4,5,6};
std::vector<int> v2 = {5,4,3,2,1,6};
std::cout << helper(v1, v2,
[](auto const & ... args){ return std::is_permutation(args...);})
<< std::endl;
std::cout << helper({1, 2, 3, 4, 5, 6}, {5, 4, 3, 2, 1, 6},
[](auto const &... args){ return std::is_permutation(args...);})
<< std::endl;
}
- 如何将 std::list of 派生类而不是 std::list of parent class 传递给函数
- std::list 模板是否需要其实例类型中的复制构造函数(或等效项)?
- constexpr constructor initialiser list 中的 constexpr 函数
- 将 std::list<std::unique_ptr> 移动到向量中会尝试引用已删除的函数
- 如何从 JNI 传递 std::list 参数来调用本机 C++ 函数
- Rcpp 中"List"的操作函数 - 类似于 R 中的"Reduce function"
- 为什么 std::list::splice 不是一个免费函数?
- 返回在函数中创建的 std::list 的第一个节点
- 按值将 std::list 从一个函数传递到另一个函数
- 将数组传递给在 arg-list 中接受双指针的函数
- 错误:没有用于初始化'List<double>::node'的匹配构造函数
- 'list::list'命名构造函数,而不是类型
- 我正在收到错误的“ list :: ShowList”:功能风格的初始化器似乎是我的CPP文件中的函数定义'
- C++ STL (std::unordered_set) 中没有用于 std::list 的哈希函数
- 没有匹配函数调用 std::list::remove_if( function()::p redicate )
- 为什么C++ std::list::clear() 不调用析构函数
- 将 std::list<boost::any> 作为函数的参数传递
- 通过 BaseClass List 调用派生类对象的析构函数
- 如何使用list::sort()对STL列表对象进行排序,同时将自定义排序函数作为参数传递给list::排序
- std::list.remove 正在调用析构函数,但不删除它