2010-04-25 12 views
5

मैं variadic टेम्पलेट्स (जीसीसी 4.5) के साथ चारों ओर खेल रहा था और इस समस्या को मारा:बूस्ट है। सी ++ 0x वैरिएडिक टेम्पलेट्स के साथ संगत?

template <typename... Args> 
boost::tuple<Args...> 
my_make_tuple(Args... args) 
{ 
    return boost::tuple<Args...>(args...); 
} 

int main (void) 
{ 
    boost::tuple<int, char> t = my_make_tuple(8, 'c'); 
} 

जीसीसी त्रुटि संदेश:

sorry, unimplemented: cannot expand 'Arg ...' into a fixed-length argument list 
In function 'int my_make_tuple(Arg ...)' 

अगर मैं std::tuple द्वारा boost::tuple के हर घटना की जगह, यह ठीक संकलित करता है।
क्या बूस्ट टुपल कार्यान्वयन में कोई समस्या है? या यह एक जीसीसी बग है?

मुझे बूस्ट के साथ रहना चाहिए। अभी के लिए अभी। क्या आप कोई कामकाज जानते हैं?
धन्यवाद।

+0

http://stackoverflow.com/questions/1989552/gcc-error-with-variadic-templates-sorry-unimplemented-cannot-expand-identif? – kennytm

उत्तर

7

यह बूस्ट के रूप में Args...T1, T2, T3, ..., T9 को विस्तारित करना प्रतीत नहीं होता है।

समाधान के लिए, निर्माणों कि इस विस्तार की आवश्यकता नहीं है का उपयोग करें:

#include <boost/tuple/tuple.hpp> 

template <typename... Args> 
auto my_make_tuple(Args... args) -> decltype(boost::make_tuple(args...)) 
{ 
    return {args...}; 
} 

int main (void) 
{ 
    boost::tuple<int, char> t = my_make_tuple(8, 'c'); 
} 

एक अन्य विकल्प मैन्युअल रूप से विस्तार हो रहा, यह देखकर कि boost::tuple 10 तर्कों का ही समर्थन करने के लिए हो सकता है।

#include <boost/tuple/tuple.hpp> 

template <unsigned, class, class...> struct nth_argument; 

template <unsigned N, class Default, class T, class... Args> 
struct nth_argument<N, Default, T, Args...> 
{ 
    typedef typename nth_argument<N - 1, Default, Args...>::type type; 
}; 

template <class Default, class T, class... Args> 
struct nth_argument<0, Default, T, Args...> 
{ 
    typedef T type; 
}; 

template <unsigned N, class Default> 
struct nth_argument<N, Default> 
{ 
    typedef Default type; 
}; 

template <typename ...Args> 
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... Args> 
typename tuple_from_var_template<Args...>::type my_make_tuple(Args... args) 
{ 
    return typename tuple_from_var_template<Args...>::type(args...); 
} 

int main (void) 
{ 
    boost::tuple<int, char> t = my_make_tuple(8, 'c'); 
} 
संबंधित मुद्दे