为什么重载成员函数访问元组和递归累积结果失败?

时间:2014-08-14 06:20:36

标签: c++ templates c++11 enable-if

我想访问元组成员并在下一个和下一个累积结果。但是没有工作,它看起来像访问越界(无效使用不完整类型struct std::tuple_element<0u, std::tuple<> >

#include <iostream>
#include <tuple>

template<typename...ARGS>
struct NextTest
{
    std::tuple<ARGS...> data;
    template<std::size_t I,bool=I<sizeof...(ARGS)>
    struct Dispatch{};
    template<std::size_t I,typename T>
    T next3(T t,Dispatch<I,false>)const
    {
        return t;
    }
    template<std::size_t I,typename T>
    auto next3(T t,Dispatch<I,true>)const->decltype(this->next3<I+1>(std::get<I>(data),Dispatch<I+1>{}))
    {

        return this->next3<I+1>(std::get<I>(data),Dispatch<I+1>{});
    }
    template<std::size_t I,typename T>
    auto next3(T t)const->decltype(this->next3<I>(t,Dispatch<I>{}))
    {
        return this->next3<I>(t,Dispatch<I>{});
    }
    template<std::size_t I,typename T,typename std::enable_if<(I>=sizeof...(ARGS))>::type* =nullptr>
    T next(T t)const
    {
        return t;
    }
    template<std::size_t I,typename T,typename std::enable_if<(I<sizeof...(ARGS))>::type* =nullptr>
    auto next(T t)const->decltype(this->next<I+1>(std::get<I>(data)))
    {

        return this->next<I+1>(std::get<I>(data));
    }
    template<std::size_t I,typename std::enable_if<(I>=sizeof...(ARGS))>::type* =nullptr>
    void next2()const
    {
        std::cout<<"end!";
    }
    template<std::size_t I,typename std::enable_if<(I<sizeof...(ARGS))>::type* =nullptr>
    void next2()const
    {
        std::cout<<"in seq ";
        next2<I+1>();
    }

};
void testexpr1()
{
    NextTest<int> nt;
    nt.next<0>( 1);//fail
nt.next3<0>( 1);//fail
nt.next2<0>();//pass!
}
mingw gcc 4.8.1为什么会出错?我该怎么办?

** update1:​​*这包括:

...\include\c++\tuple||In instantiation of 'struct std::tuple_element<1u, std::tuple<int> >':|
...\include\c++\tuple|771|  required by substitution of 'template<unsigned int __i, class ... _Elements> constexpr typename std::__add_r_ref<typename std::tuple_element<__i, std::tuple<_Elements ...> >::type>::type std::get(std::tuple<_Elements ...>&&) [with unsigned int __i = 1u; _Elements = {int}]'|
...\test.cpp|83|  required by substitution of 'template<unsigned int I, class T> decltype (this->.next1<(I + 1)>(get<I>(this->.data))) NextTest<ARGS>::next1(T&, typename std::enable_if<(I < sizeof (ARGS ...))>::type*) [with unsigned int I = I; T = T; ARGS = {int}] [with unsigned int I = 1u; T = <missing>]'|
...\test.cpp|83|  required by substitution of 'template<unsigned int I, class T> decltype (this->.next1<(I + 1)>(get<I>(this->.data))) NextTest<ARGS>::next1(T&, typename std::enable_if<(I < sizeof (ARGS ...))>::type*) [with unsigned int I = I; T = T; ARGS = {int}] [with unsigned int I = 0u; T = int]'|
...\test.cpp|104|required from here|
...\include\c++\tuple|680|error: invalid use of incomplete type 'struct std::tuple_element<0u, std::tuple<> >'|
...\include\c++\utility|84|error: declaration of 'struct std::tuple_element<0u, std::tuple<> >'|

1 个答案:

答案 0 :(得分:1)

代码中似乎有两个错误:

  • 一个是@Nawaz提到这个 - &gt;在decltype部分中需要访问成员函数和成员变量(至少在gcc 4.7.2中)。
  • 第二个是访问元组元素的off-by-one error(请参阅enable_if元函数调用中'I'更改为'I + 1'的位置以及Dispatch结构中的默认参数)。

修复这两个问题后,代码在gcc 4.7.2上编译得很好。

这是固定代码:

#include <iostream>
#include <tuple>

template<typename...ARGS>
struct NextTest
{
    std::tuple<ARGS...> data;

    template<std::size_t I,bool=(I+1)<sizeof...(ARGS)>
    struct Dispatch{};

    template<std::size_t I,typename T>
    T next3(T t,Dispatch<I,false>)const
    {
        return t;
    }
    template<std::size_t I,typename T>
    auto next3(T t,Dispatch<I,true>)const->decltype(this->next3<I+1>(std::get<I>(this->data),Dispatch<I+1>{}))
    {
        return this->next3<I+1>(std::get<I>(data),Dispatch<I+1>{});
    }
    template<std::size_t I,typename T>
    auto next3(T t)const->decltype(this->next3<I>(t,Dispatch<I>{}))
    {
        return this->next3<I>(t,Dispatch<I>{});
    }

    template<std::size_t I,typename T,typename std::enable_if<((I+1)>=sizeof... (ARGS))>::type* =nullptr>
    T next(T t)const
    {
        return t;
    }
    template<std::size_t I,typename T,typename std::enable_if<((I+1)<sizeof...(ARGS))>::type* =nullptr>
    auto next(T t)const->decltype(this->next<I+1>(std::get<I>(this->data)))
    {
        return this->next<I+1>(std::get<I>(data));
    }

    template<std::size_t I,typename std::enable_if<((I+1)>=sizeof...(ARGS))>::type* =nullptr>
    void next2()const
    {
        std::cout<<"end!";
    }
    template<std::size_t I,typename std::enable_if<((I+1)<sizeof...(ARGS))>::type* =nullptr>
    void next2()const
    {
        std::cout<<"in seq ";
        next2<I+1>();
    }
};

void testexpr1()
{
    NextTest<int> nt;
    nt.next<0>( 1);//pass
    nt.next3<0>( 1);//pass
    nt.next2<0>();//pass!
}