Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 多变量模板函数_C++_C++11_Templates_Variadic Templates_Variadic Functions - Fatal编程技术网

C++ 多变量模板函数

C++ 多变量模板函数,c++,c++11,templates,variadic-templates,variadic-functions,C++,C++11,Templates,Variadic Templates,Variadic Functions,假设我有两个函数f1()和f2(),定义如下: template<class ... Types> void f1(Types ... args1) template<class ... Types> void f2(Types ... args2) 你不能 因为模板函数是一组函数。你可以传递一个函数,解释模板类型 f3(f1<int, long, long>, f2<char, int>); 或者仅仅是类型 f3<s1, s2>(

假设我有两个函数
f1()
f2()
,定义如下:

template<class ... Types> void f1(Types ... args1)
template<class ... Types> void f2(Types ... args2)
你不能

因为模板函数是一组函数。你可以传递一个函数,解释模板类型

f3(f1<int, long, long>, f2<char, int>);
或者仅仅是类型

f3<s1, s2>();

我的意思是当我调用f3时,我提供f1和f2。我还需要传入f1和f2的参数。我该怎么做

但是
f1()
f2()
的参数是相同的吗? 还是两套不同

在第一种情况下,可以将它们作为模板变量参数传递;(按照Jarod42的建议)

但是请注意,
std::apply()
仅在C++17中可用:之前从元组中提取参数要复杂一些

如果没有c++17,我怎么做

下面是一个完整的C++14示例,它使用
std::index_sequence\u对
std::index_sequence

#include <tuple>
#include <iostream>
#include <type_traits>

template <typename ... Ts>
void f1 (Ts ... as)
 { std::cout << "- f1: " << sizeof...(Ts) << " arguments" << std::endl; }

template <typename ... Ts>
void f2 (Ts ... as)
 { std::cout << "- f2: " << sizeof...(Ts) << " arguments" << std::endl; }

template <typename L, typename ... Ts, std::size_t ... Is>
void f3_helper (L l, std::tuple<Ts...> const & t, std::index_sequence<Is...>)
 { l(std::get<Is>(t)...); }

template <typename L1, typename L2, typename ... As1, typename ... As2>
void f3 (L1 l1, L2 l2, std::tuple<As1...> const & t1,
         std::tuple<As2...> const & t2)
 {
   f3_helper(l1, t1, std::index_sequence_for<As1...>{});
   f3_helper(l2, t2, std::index_sequence_for<As2...>{});
 }  

int main()
 {
   auto l1 = [](auto && ... args)
    { f1(std::forward<decltype(args)>(args)...); };

   auto l2 = [](auto && ... args)
    { f2(std::forward<decltype(args)>(args)...); };

   f3(l1, l2, std::make_tuple(1, 2l, '3'), std::make_tuple('4', 5ull));
 }
#包括
#包括
#包括
模板
无效f1(Ts…as)
{std::cout你不能

因为模板函数是一组函数。你可以传递一个函数,解释模板类型

f3(f1<int, long, long>, f2<char, int>);
或者仅仅是类型

f3<s1, s2>();

我的意思是,当我调用f3时,我提供f1和f2。我还需要传递f1和f2的参数。我该怎么做

但是
f1()
f2()
的参数是相同的吗? 还是两套不同

在第一种情况下,您可以将它们作为模板变量参数传递;例如(按照Jarod42的建议)

但是请注意,
std::apply()
仅在C++17中可用:之前从元组中提取参数要复杂一些

如果没有c++17,我怎么做

下面是一个完整的C++14示例,它使用
std::index_sequence\u对
std::index_sequence

#include <tuple>
#include <iostream>
#include <type_traits>

template <typename ... Ts>
void f1 (Ts ... as)
 { std::cout << "- f1: " << sizeof...(Ts) << " arguments" << std::endl; }

template <typename ... Ts>
void f2 (Ts ... as)
 { std::cout << "- f2: " << sizeof...(Ts) << " arguments" << std::endl; }

template <typename L, typename ... Ts, std::size_t ... Is>
void f3_helper (L l, std::tuple<Ts...> const & t, std::index_sequence<Is...>)
 { l(std::get<Is>(t)...); }

template <typename L1, typename L2, typename ... As1, typename ... As2>
void f3 (L1 l1, L2 l2, std::tuple<As1...> const & t1,
         std::tuple<As2...> const & t2)
 {
   f3_helper(l1, t1, std::index_sequence_for<As1...>{});
   f3_helper(l2, t2, std::index_sequence_for<As2...>{});
 }  

int main()
 {
   auto l1 = [](auto && ... args)
    { f1(std::forward<decltype(args)>(args)...); };

   auto l2 = [](auto && ... args)
    { f2(std::forward<decltype(args)>(args)...); };

   f3(l1, l2, std::make_tuple(1, 2l, '3'), std::make_tuple('4', 5ull));
 }
#包括
#包括
#包括
模板
无效f1(Ts…as)

{std::cout在这种情况下,我如何将参数传递给f1和f2?@WhatABeautifulWorld-你的意思是:修复一组精确的参数类型吗?我的意思是,当我调用f3时,我提供f1和f2。我还需要传递f1和f2的参数。我该如何做?@WhatABeautifulWorld-“如果我没有std::apply,我该怎么做?”-通过
std::make_index_sequence
std::index_sequence
(但C++14是必需的;在C++11中,您必须编写一个替换项,但并不难);给我几分钟,我准备一个例子。@WhatABeautifulWorld-答案改进了。在这种情况下,我如何将参数传递给f1和f2?@WhatABeautifulWorld-你的意思是:修复一组精确的参数吗?我的意思是,当我调用f3时,我提供f1和f2。我还需要传递f1和f2的参数。我该怎么做?@WhatABeautifulWorld—“如果我没有std::apply,我该怎么做?”—通过
std::make_index_sequence
std::index_sequence
(但C++14是必需的;在C++11中,您必须编写一个替代项,但这并不困难);给我几分钟,我准备一个例子。@WhatABeautifulWorld-答案改进了。你能解释一下你的预期行为吗?而且你用两种方式使用名称
f1
f2
很让人困惑-你想
f3
调用传递给它的函数,还是调用上面声明的实际命名为
f1
的函数?这两件事有什么关系?@aschepler what max66很好地回答了我的问题。我有两个函数:f1和f2,都是可变模板函数。现在我想把它们传递给一个新函数f3。同时,我还需要把f1和f2的参数传递给f3。如果f1和f2共享相同的参数,这很容易。但在我的例子中,它们是这样做的不能。你能解释一下你的预期行为吗?而且你用两种方式使用名称
f1
f2
,这让人很困惑——你想
f3
调用传递给它的函数,还是上面声明的实际命名为
f1
的函数?这两个东西有什么关系?@aschepler what max66补充的内容回答了我的问题我有两个函数:f1和f2,都是可变模板函数。现在我想把它们传递给一个新函数f3。同时,我还需要把f1和f2的参数传递给f3。如果f1和f2共享相同的参数,这很容易。但在我的例子中,它们没有。
template <typename L1, typename L2, typename ... Args>
void f3 (L1 l1, L2 l2, Args const & ... as)
 {
   L1(as...);
   L2(as...);
 }  

auto l1 = [](auto&& ... args) { f1(std::forward<decltype(args)>(args)...); };
auto l2 = [](auto&& ... args) { f2(std::forward<decltype(args)>(args)...); };

f3(l1, l2, 1, '2', 3L, 4ULL);
template <typename L1, typename L2, typename ... As1, typename ... As2>
void f3 (L1 l1, L2 l2, std::tuple<As1...> const & t1, std::tuple<As2...> const & t2)
 {
   std::apply(l1, t1);
   std::apply(l2, t2);
 }  

auto l1 = [](auto&& ... args) { f1(std::forward<decltype(args)>(args)...); };
auto l2 = [](auto&& ... args) { f2(std::forward<decltype(args)>(args)...); };

f3(l1, l2, std::make_tuple(1, 2l, '3'), std::make_tuple('4', 5ull));
#include <tuple>
#include <iostream>
#include <type_traits>

template <typename ... Ts>
void f1 (Ts ... as)
 { std::cout << "- f1: " << sizeof...(Ts) << " arguments" << std::endl; }

template <typename ... Ts>
void f2 (Ts ... as)
 { std::cout << "- f2: " << sizeof...(Ts) << " arguments" << std::endl; }

template <typename L, typename ... Ts, std::size_t ... Is>
void f3_helper (L l, std::tuple<Ts...> const & t, std::index_sequence<Is...>)
 { l(std::get<Is>(t)...); }

template <typename L1, typename L2, typename ... As1, typename ... As2>
void f3 (L1 l1, L2 l2, std::tuple<As1...> const & t1,
         std::tuple<As2...> const & t2)
 {
   f3_helper(l1, t1, std::index_sequence_for<As1...>{});
   f3_helper(l2, t2, std::index_sequence_for<As2...>{});
 }  

int main()
 {
   auto l1 = [](auto && ... args)
    { f1(std::forward<decltype(args)>(args)...); };

   auto l2 = [](auto && ... args)
    { f2(std::forward<decltype(args)>(args)...); };

   f3(l1, l2, std::make_tuple(1, 2l, '3'), std::make_tuple('4', 5ull));
 }