boost::tuple with uniform types

A

Alex Vinokur

Here is some tuple (triple in this case) with uniform types (for
instance, double):
boost::tuple<double, double, double> t;

Is there any way (in boost::tuple) to define such tuples something
like
uniform_tuple <3, double> t; ?

Alex Vinokur
 
M

mlimber

Here is some tuple (triple in this case) with uniform types (for
instance, double):
boost::tuple<double, double, double> t;

Is there any way (in boost::tuple) to define such tuples something
like
uniform_tuple <3, double> t; ?

Not inherently, but you could use your own template typedefs:

template<int count, typename T> struct uniform_tuple;
template<typename T> struct uniform_tuple<2,T> { typedef
boost::tuple<T,T> type; };
template<typename T> struct uniform_tuple<3,T> { typedef
boost::tuple<T,T,T> type; };
template<typename T> struct uniform_tuple<4,T> { typedef
boost::tuple<T,T,T,T> type; };
// etc.

Then you use it like:

uniform_tuple<3,double>::type t;

You could play with Boost's MPL type lists and such to make it more
generic and have the preprocessor generate the redundant code, but
that may be overkill for what you want to do (not to mention beyond
the scope of this group, unlike tuple talk). Also the next version of
the standard will make template typedefs of thing a little prettier,
but you'll have to live with the ugliness for now.

Cheers! --M
 
H

Howard Hinnant

Not inherently, but you could use your own template typedefs:

template<int count, typename T> struct uniform_tuple;
template<typename T> struct uniform_tuple<2,T> { typedef
boost::tuple<T,T>     type; };
template<typename T> struct uniform_tuple<3,T> { typedef
boost::tuple<T,T,T>   type; };
template<typename T> struct uniform_tuple<4,T> { typedef
boost::tuple<T,T,T,T> type; };
// etc.

Then you use it like:

uniform_tuple<3,double>::type t;

You could play with Boost's MPL type lists and such to make it more
generic and have the preprocessor generate the redundant code, but
that may be overkill for what you want to do (not to mention beyond
the scope of this group, unlike tuple talk). Also the next version of
the standard will make template typedefs of thing a little prettier,
but you'll have to live with the ugliness for now.

Cheers! --M

Just by coincidence I had just written the following code. It doesn't
help the OP because it uses C++0X features (variadic templates). So
this is just a tantalizing peek at the future as opposed to genuine
help. :-\ But I thought it might be interesting nonetheless.

template <class Tuple, class U, std::size_t N> struct
make_array_imp;

template <class ...Up, class U>
struct make_array_imp<std::tuple<Up...>, U, 0>
{
typedef std::tuple<Up...> type;
};

template <class ...Up, class U, std::size_t N>
struct make_array_imp<std::tuple<Up...>, U, N>
{
typedef typename make_array_imp<std::tuple<Up..., U>, U,
N-1>::type type;
};

template <class U, std::size_t N>
struct make_array
{
typedef typename make_array_imp<std::tuple<>, U, N>::type
type;
};

typedef typename make_array<T, 3>::type array; // tuple<T, T, T>

-Howard
 
H

Howard Hinnant

Why not abbreviate to std::array<T,N>?

I am hopeful that will be a good solution. std::array is currently
lacking a move constructor and the ability to interoperate with tuple
and pair (other tuple-like types).

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top