C++ std::tuple_cat替换失败

C++ std::tuple_cat替换失败,c++,c++11,tuples,C++,C++11,Tuples,我使用std::tuple\u cat将参数列表的子集选择为元组,如下所示: template <class...> struct odds; template <class T1> struct odds<T1> { typedef std::tuple<T1> type; static type value(T1&& t1) { return std::make_tuple(std::f

我使用
std::tuple\u cat
将参数列表的子集选择为元组,如下所示:

template <class...>
struct odds;

template <class T1>
struct odds<T1>
{
    typedef std::tuple<T1> type;
    static type value(T1&& t1)
    {
        return std::make_tuple(std::forward<T1>(t1));
    }
};

template <class T1, class T2>
struct odds<T1, T2>
{
    typedef std::tuple<T1> type;
    static type value(T1&& t1, T2&&)
    {
        return std::make_tuple(std::forward<T1>(t1));
    }
};

template <class T1, class T2, class... TTail> 
struct odds<T1, T2, TTail...>
{
        typedef decltype(std::tuple_cat(T1(), typename odds<TTail...>::type())) type; // L32
        static type value(T1&& t1, T2&&, TTail&&... rest)
    {
        return std::tuple_cat(std::forward<T1>(t1), odds<TTail...>::value(std::forward<TTail>(rest)...)); // L35
    }
};
模板
结构几率;
模板
结构赔率
{
typedef std::元组类型;
静态类型值(T1&&T1)
{
返回std::make_tuple(std::forward(t1));
}
};
模板
结构赔率
{
typedef std::元组类型;
静态类型值(T1&&T1、T2&&T)
{
返回std::make_tuple(std::forward(t1));
}
};
模板
结构赔率
{
typedef decltype(std::tuple_cat(T1(),typename::type())类型;//L32
静态类型值(T1&&T1、T2&&T、TTail&&T…rest)
{
返回std::tuple_cat(std::forward(t1),赔率::value(std::forward(rest)…);//L35
}
};
,并将以下内容作为测试用例:

// assume <tuple>, <utility> are included at top of file
template <class... T>
auto foo(T... x) -> typename odds<T...>::type
{
         return odds<T...>::value(x...);
         //...
 }       
 int main() {
         auto bar = foo(5, true, 6, false); // L46
         auto baz = odds<int, bool, int, bool>::value(5, true, 6, false); // L47
         // bar, baz should be tuple<int,int> with value { 5, 6 }
 }
//假定,包含在文件顶部
模板
自动foo(T…x)->typename赔率::type
{
返回概率::值(x…);
//...
}       
int main(){
自动条=foo(5,真,6,假);//L46
auto baz=赔率::值(5,真,6,假);//L47
//baz应该是值为{5,6}的元组
}
但是,clang-3.1和gcc-4.7.2中都出现了模板推导问题:

叮当声输出:

test.cc:32:19: error: no matching function for call to 'tuple_cat'
        typedef decltype(std::tuple_cat(T1(), typename odds<TTail...>::type())) type;
                         ^~~~~~~~~~~~~~
test.cc:40:30: note: in instantiation of template class 'odds<int, bool, int, bool>' requested here
auto foo(T... x) -> typename odds<T...>::type
                             ^
test.cc:40:6: note: while substituting deduced template arguments into function template 'foo' [with T = <int, bool, int, bool>]
auto foo(T... x) -> typename odds<T...>::type
     ^
/usr/include/c++/v1/tuple:1063:1: note: candidate template ignored: substitution failure [with _Tuple0 = int, _Tuples = <std::__1::tuple<int>>]
tuple_cat(_Tuple0&& __t0, _Tuples&&... __tpls)
^
/usr/include/c++/v1/tuple:987:1: note: candidate function not viable: requires 0 arguments, but 2 were provided
tuple_cat()
^
test.cc:46:13: error: no matching function for call to 'foo'
        auto bar = foo(5, true, 6, false);
                   ^~~
test.cc:40:6: note: candidate template ignored: substitution failure [with T = <int, bool, int, bool>]
auto foo(T... x) -> typename odds<T...>::type
     ^
test.cc:35:10: error: no matching function for call to 'tuple_cat'
                return std::tuple_cat(std::forward<T1>(t1), odds<TTail...>::value(std::forward<TTail>(rest)...));
                       ^~~~~~~~~~~~~~
test.cc:47:41: note: in instantiation of member function 'odds<int, bool, int, bool>::value' requested here
        auto baz = odds<int, bool, int, bool>::value(5,true,6,false);
                                               ^
/usr/include/c++/v1/tuple:1063:1: note: candidate template ignored: substitution failure [with _Tuple0 = int, _Tuples = <std::__1::tuple<int>>]
tuple_cat(_Tuple0&& __t0, _Tuples&&... __tpls)
^
/usr/include/c++/v1/tuple:987:1: note: candidate function not viable: requires 0 arguments, but 2 were provided
tuple_cat()
^
3 errors generated.
test.cc:32:19:错误:对“tuple\u cat”的调用没有匹配的函数
typedef decltype(std::tuple_cat(T1(),typename::type())类型;
^~~~~~~~~~~~~~
test.cc:40:30:注意:在这里请求的模板类“赔率”的实例化中
自动foo(T…x)->typename赔率::type
^
test.cc:40:6:注意:在将推导出的模板参数替换为函数模板'foo'时[使用T=]
自动foo(T…x)->typename赔率::type
^
/usr/include/c++/v1/tuple:1063:1:注意:已忽略候选模板:替换失败[with _Tuple0=int,_Tuples=]
元组猫(_Tuple0&&&uuuu t0,_Tuples&&uu tpls)
^
/usr/include/c++/v1/tuple:987:1:注意:候选函数不可行:需要0个参数,但提供了2个
tuple_cat()
^
test.cc:46:13:错误:调用“foo”时没有匹配的函数
自动条=foo(5,真,6,假);
^~~
test.cc:40:6:注意:候选模板被忽略:替换失败[with T=]
自动foo(T…x)->typename赔率::type
^
test.cc:35:10:错误:对“tuple\u cat”的调用没有匹配的函数
返回std::tuple_cat(std::forward(t1),赔率::value(std::forward(rest)…);
^~~~~~~~~~~~~~
test.cc:47:41:注意:在这里请求的成员函数'Lobbs::value'的实例化中
auto baz=赔率::值(5,真,6,假);
^
/usr/include/c++/v1/tuple:1063:1:注意:已忽略候选模板:替换失败[with _Tuple0=int,_Tuples=]
元组猫(_Tuple0&&&uuuu t0,_Tuples&&uu tpls)
^
/usr/include/c++/v1/tuple:987:1:注意:候选函数不可行:需要0个参数,但提供了2个
tuple_cat()
^
产生了3个错误。
Gcc输出:

test.cc: In instantiation of ‘struct odds<int, bool, int, bool>’:
test.cc:40:6:   required by substitution of ‘template<class ... T> typename odds<T ...>::type foo(T ...) [with T = {int, bool, int, bool}]’
test.cc:46:34:   required from here
test.cc:32:74: error: no matching function for call to ‘tuple_cat(int, odds<int, bool>::type)’
test.cc:32:74: note: candidate is:
In file included from test.cc:1:0:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.2/include/g++-v4/tuple:1027:5: note: template<class ... _Tpls, class> constexpr typename std::__tuple_cat_result<_Tpls ...>::__type std::tuple_cat(_Tpls&& ...)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.2/include/g++-v4/tuple:1027:5: note:   template argument deduction/substitution failed:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.2/include/g++-v4/tuple:1024:31: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
test.cc: In function ‘int main()’:
test.cc:46:34: error: no matching function for call to ‘foo(int, bool, int, bool)’
test.cc:46:34: note: candidate is:
test.cc:40:6: note: template<class ... T> typename odds<T ...>::type foo(T ...)
test.cc:40:6: note:   substitution of deduced template arguments resulted in errors seen above
test.cc:46:34: error: unable to deduce ‘auto’ from ‘<expression error>’
test.cc:47:13: error: ‘value’ is not a member of ‘odds<int, bool, int, bool>’
test.cc:47:61: error: unable to deduce ‘auto’ from ‘<expression error>’
test.cc:在“struct赔率”的实例化中:
test.cc:40:6:替换“模板typename赔率::type foo(T…[with T={int,bool,int,bool}]时需要”
测试。cc:46:34:此处需要
test.cc:32:74:错误:对“tuple_cat(int,赔率::type)”的调用没有匹配的函数
测试cc:32:74:注:候选人为:
在test.cc中包含的文件中:1:0:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.2/include/g++-v4/tuple:1027:5:注意:模板constepr typename std::uu tuple_cat结果::uu type std::tuple_cat(Tpls&…)
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.2/include/g++-v4/tuple:1027:5:注意:模板参数推断/替换失败:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.2/include/g++-v4/tuple:1024:31:错误:在“struct std::enable_if”中没有名为“type”的类型
test.cc:在函数“int main()”中:
test.cc:46:34:错误:对“foo(int,bool,int,bool)”的调用没有匹配的函数
测试cc:46:34:注:候选人为:
test.cc:40:6:注意:模板typename赔率::type foo(T…)
test.cc:40:6:注意:替换推导的模板参数会导致上面所示的错误
test.cc:46:34:错误:无法从“”推断“自动”
test.cc:47:13:错误:“value”不是“赔率”的成员
test.cc:47:61:错误:无法从“”推断“自动”
Gcc在这里更有帮助,特别是在错误方面

    test.cc:32:74: error: no matching function for call to ‘tuple_cat(int, odds<int, bool>::type)’
test.cc:32:74:错误:调用“tuple_cat(int,赔率::type)”时没有匹配函数
目标是调用一个函数,该函数递归地解压参数,将选择收集到一个收集元组中,然后返回它。为了以平坦的方式累积它,我使用
std::tuple\u cat()
来平坦递归尾部元组,添加一个头部,然后返回元组。转发用于在递归期间不删除引用限定符

在代码的后面,结果元组被解包以调用不同的变量函数,但这超出了此错误的范围


显然,我遗漏了一些微妙但关键的细节,但我发现在追踪根本问题时有很大的困难。

tuple\u cat的参数必须是tuple(或“类似tuple的”支持
std::tuple_size
std::tuple_元素
API的东西,例如
std::pair
std::array
),但从错误消息中,您似乎在使用非元组类型调用它。要做到这一点,您可能需要:

std::tuple_cat(std::make_tuple(a_non_tuple), a_tuple, another_tuple);
这会将第一个参数转换为一个
元组
,因此它可以与其他元组连接

std::tuple
连接到
std::tuple

这不是“串联”。我认为正确的说法是“扁平化”

我不太确定你在尝试什么,因为你没有提供一个完整的例子。您是否使用类似于
std::tuple
的参数调用
foo

那不行,因为
foo
namespace skip_args {
namespace detail {

// We use Tuple as a kind of generic typelist.
template<typename ...T>
using Tuple = std::tuple<T...>;

// Utility "functions"
// Pusher<T, Tuple<U...> >::type = Tuple<T, U...>
template<typename A, typename B> struct Pusher;
template<typename T, typename ...U> struct Pusher<T, Tuple<U...>> {
  using type = Tuple<T, U...>;
};
template<typename A, typename B>
using push = typename Pusher<A, B>::type;

// Skip is an intermediate used to skip over ignored elements.
// To allow recursion, we declare the general form first.
// All three arguments are Tuples.
template<typename Next, typename Rest, typename This> struct Skip;

// Node is actually used to store some data item. It also inherits from the next
// following Node (if there is one) so that we end up with an inheritance chain.
// (That's the part similar to libstdc++ tuples; it makes the layout the same,
// too, as long as there's no EBO to deal with, because we don't bother here.)
template<typename Rest, typename ...This> struct Node;

template<typename ...R_, typename T, typename ...T_>
struct Node<Tuple<R_...>, T, T_...>
: Skip<Tuple<>, Tuple<R_...>, Tuple<T, T_...>>::type {
  using self = Node<Tuple<R_...>, T, T_...>;
  using next = typename Skip<Tuple<>, Tuple<R_...>, Tuple<T, T_...>>::type;
  // Recursive construction of node types
  using nodes = push<self, typename next::nodes>;
  using value_type = T;
  // The constructor takes all the arguments, uses the first one, skips some,
  // and passes the rest to the next node.
  constexpr Node<Tuple<R_...>, T, T_...>(T&& t, T_&&...t_, R_&&...r_)
      : next(std::forward<R_>(r_)...), value(std::forward<T>(t)) {
  }
  T&& value;
};

// Base:
template<>
struct Node<Tuple<>> {
  using nodes = Tuple<>;
  constexpr Node<Tuple<>>() {}
};

// Skip (N...) (R R...) (T T...) => Skip (N... R) (R...) (T...)
// In other words, it drops elements from the third tuple, and for each one it
// moves an element from the second tuple to the first tuple. If it runs out of
// the third tuple, it "returns" a new Node. If it runs out of the second tuple,
// then we're done, but to satisfy the node requirements, it actually needs to
// declare a constructor (which drops all its arguments)

// General case:
template<typename ...N_, typename R, typename ...R_, typename T, typename ...T_>
struct Skip<Tuple<N_...>, Tuple<R, R_...>, Tuple<T, T_...>>
  : Skip<Tuple<N_..., R>, Tuple<R_...>, Tuple<T_...>> {
};
// Ran out of pattern
template<typename ...N_, typename R_, typename T_>
struct Skip<Tuple<N_...>, R_, T_> {
  using type = Node<R_, N_...>;
  using nodes = typename type::nodes;
};

template<typename T> struct TupleTyper;
template<typename ...T> struct TupleTyper<Tuple<T...>> {
  using type = Tuple<typename T::value_type...>;
};

template<typename A, typename B, typename C> struct TupleMaker;
template<typename Tup, typename H, typename ...T>
struct TupleMaker<Tup, H, Tuple<T...>> {
  Tup operator()(H&& helper) {
    return Tup(static_cast<T&>(helper).value...);
  }
};

template<typename N> struct SkipHelper {
  using tuple_type = typename TupleTyper<typename N::nodes>::type;
  template<typename ...U>
  tuple_type operator()(U&& ...u) {
    return TupleMaker<tuple_type, N, typename N::nodes>()(N(std::forward<U>(u)...));
  }
};

} // namespace detail

template<typename ...T> struct Odds;

template<typename T1, typename T2, typename ...T_>
struct Odds<T1, T2, T_...>
  : detail::SkipHelper<detail::Node<detail::Tuple<T_...>, T1, T2>> {
};
template<typename T1>
struct Odds<T1> : detail::SkipHelper<detail::Node<detail::Tuple<>, T1>> {
};

// tuple_from_odds takes any number of arguments,
// and returns a tuple of the odd numbered ones.
template<typename...T> auto tuple_from_odds(T&&...t)
    -> typename Odds<T...>::tuple_type {
  return Odds<T...>()(std::forward<T>(t)...);
}

} // namespace skip_args