tuple<T,T,...> generator meta-function

Hi folks, How can I do this in a better way? template <typename T, size_t N> struct make_tuple { }; template <typename T> struct make_tuple<T, 1> { typedef boost::tuple<T> type; }; template <typename T> struct make_tuple<T, 2> { typedef boost::tuple<T, T> type; }; template <typename T> struct make_tuple<T, 3> { typedef boost::tuple<T, T, T> type; }; template <typename T> struct make_tuple<T, 4> { typedef boost::tuple<T, T, T, T> type; }; // ... Thanks, emre

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Friday 23 October 2009, Emre Turkay wrote:
Hi folks,
How can I do this in a better way?
template <typename T, size_t N> struct make_tuple { };
template <typename T> struct make_tuple<T, 1> { typedef boost::tuple<T> type; };
template <typename T> struct make_tuple<T, 2> { typedef boost::tuple<T, T> type; };
If you have a c++0x compiler, you could define it recursively using variadic templates. Otherwise, you could use Boost.Preprocessor. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkrhrJkACgkQ5vihyNWuA4WOIQCeLggKvmAlYFzRsYWXSwiqd5tx JJkAnikG50kPcfpo86Whp8UVZoGWZ9yF =BXDu -----END PGP SIGNATURE-----

Both will work, if you want different types generated. I think Emre wants the same type generated for T, not T0, T1, T2..... as the preprocessor & variadics would do. Pass make_tuple<int, 8>, & it generates a tuple of 8 ints. However, if otherwise, & no access to C++ 0x: #include <boost/preprocessor/repetition/enum_params.hpp> template<size_t VAL, size_t LIMIT> struct limit_repeat { enum { value = LIMIT < VAL ? LIMIT : VAL }; }; template <typename T, size_t N, size_t M> struct make_tuple { }; template <typename T> struct make_tuple<T, N = 1, M = limit_repeat<N, BOOST_PP_LIMIT_REPEAT>::value > { typedef boost::tuple<BOOST_PP_ENUM_PARAMS(M, T)> type; }; ______________________________________________________________________ Kizza George Mbidde Interconnect Billing Systems Analyst IT cell: +256 77 212 0982 email: mbiddeg@mtn.co.ug -----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Frank Mori Hess Sent: October 23, 2009 16:16 To: boost@lists.boost.org Subject: Re: [boost] tuple<T,T,...> generator meta-function -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Friday 23 October 2009, Emre Turkay wrote:
Hi folks,
How can I do this in a better way?
template <typename T, size_t N>
struct make_tuple { };
template <typename T>
struct make_tuple<T, 1>
{
typedef boost::tuple<T> type;
};
template <typename T>
struct make_tuple<T, 2>
{
typedef boost::tuple<T, T> type;
};
If you have a c++0x compiler, you could define it recursively using variadic templates. Otherwise, you could use Boost.Preprocessor. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkrhrJkACgkQ5vihyNWuA4WOIQCeLggKvmAlYFzRsYWXSwiqd5tx JJkAnikG50kPcfpo86Whp8UVZoGWZ9yF =BXDu -----END PGP SIGNATURE----- _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost No virus found in this incoming message. Checked by AVG - www.avg.com Version: 8.5.423 / Virus Database: 270.14.27/2452 - Release Date: 10/22/09 18:44:00
participants (4)
-
Emre Turkay
-
Frank Mori Hess
-
mbiddeg@mtn.co.ug
-
Russell Goyder