Is Boost.Tuple compatible with C++0x variadic templates?

前端 未结 1 1423
醉梦人生
醉梦人生 2021-01-02 23:52

I was playing around with variadic templates (gcc 4.5) and hit this problem :

template 
boost::tuple
my_make_tuple(Arg         


        
1条回答
  •  清酒与你
    2021-01-03 00:05

    It doesn't seem to like expanding Args... to T1, T2, T3, ..., T9 as Boost has it.

    As a workaround, use constructs that don't require this expansion:

    #include 
    
    template 
    auto my_make_tuple(Args... args) -> decltype(boost::make_tuple(args...))
    {
       return {args...};
    }
    
    int main (void)
    {
        boost::tuple t = my_make_tuple(8, 'c');
    }
    

    Another option might be to do the expanding manually, seeing that boost::tuple supports up to 10 arguments.

    #include 
    
    template  struct nth_argument;
    
    template 
    struct nth_argument
    {
        typedef typename nth_argument::type type;
    };
    
    template 
    struct nth_argument<0, Default, T, Args...>
    {
        typedef T type;
    };
    
    template 
    struct nth_argument
    {
        typedef Default type;
    };
    
    template 
    struct tuple_from_var_template
    {
        typedef boost::tuple<
            typename nth_argument<0, boost::tuples::null_type, Args...>::type,
            typename nth_argument<1, boost::tuples::null_type, Args...>::type,
            typename nth_argument<2, boost::tuples::null_type, Args...>::type,
            typename nth_argument<3, boost::tuples::null_type, Args...>::type,
            typename nth_argument<4, boost::tuples::null_type, Args...>::type,
            typename nth_argument<5, boost::tuples::null_type, Args...>::type,
            typename nth_argument<6, boost::tuples::null_type, Args...>::type,
            typename nth_argument<7, boost::tuples::null_type, Args...>::type,
            typename nth_argument<8, boost::tuples::null_type, Args...>::type,
            typename nth_argument<9, boost::tuples::null_type, Args...>::type
        > type;
    };
    
    template 
    typename tuple_from_var_template::type my_make_tuple(Args... args)
    {
       return typename tuple_from_var_template::type(args...);
    }
    
    int main (void)
    {
        boost::tuple t = my_make_tuple(8, 'c');
    }
    

    0 讨论(0)
提交回复
热议问题