|
Boost Users : |
Subject: Re: [Boost-users] a tuple from a pair?
From: Joel de Guzman (joel_at_[hidden])
Date: 2011-10-05 19:57:06
On 10/6/2011 3:07 AM, Krzysztof Żelechowski wrote:
> Krzysztof Żelechowski wrote:
>
>> How come a tuple, being a generalisation of a pair, cannot be constructed
>> from one?
>>
>> I mean something along the lines
>>
>> make_tuple (pair const &p) { return make_tuple (p.first, p_second); }
>>
>> Would be good to have.
>
> It turns out that TR1 supports constructing a tuple directly from a pair,
> so Boost should catch up.
Boost, is at the forefront of C++ dev and in fact spearheaded the
development of many std libraries, including tuple. FYI, there's
a more powerful tuples library called boost.fusion that includes a
(simple) TR1 tuple implementation.
Here's how you do it in fusion:
#include <boost/fusion/tuple.hpp>
#include <boost/fusion/adapted/std_pair.hpp>
#include <iostream>
int main()
{
using boost::fusion::tuple;
using std::pair;
pair<int, int> p(1, 2);
tuple<int, int> t = p;
std::cout << t << std::endl;
return 0;
}
But fusion gives you more than that and beyond TR1. It's the
unification of all kinds of "tuples" you can imagine. For example:
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/sequence/io.hpp>
#include <boost/fusion/adapted/boost_array.hpp>
#include <boost/array.hpp>
#include <iostream>
int main()
{
// fusion vector is TR1 tuples in steroids
using boost::fusion::vector;
using boost::array;
array<int, 2> p = { 1, 2 };
vector<int, int> t = p;
std::cout << t << std::endl;
return 0;
}
Or how about:
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/sequence/io.hpp>
#include <boost/fusion/adapted/struct.hpp>
#include <iostream>
// user defined struct
struct my_struct
{
int a;
int b;
};
BOOST_FUSION_ADAPT_STRUCT(my_struct,
(int, a)
(int, b)
);
int main()
{
// fusion vector is TR1 tuples in steroids
using boost::fusion::vector;
my_struct p = { 1, 2 };
vector<int, int> t = p;
std::cout << t << std::endl;
return 0;
}
See http://tinyurl.com/3tx6kys for more.
Regards,
-- Joel de Guzman http://www.boostpro.com http://boost-spirit.com
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net