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
#include
#include <iostream>
int main()
{
using boost::fusion::tuple;
using std::pair;
pair p(1, 2);
tuple 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
#include
#include
#include
#include <iostream>
int main()
{
// fusion vector is TR1 tuples in steroids
using boost::fusion::vector;
using boost::array;
array p = { 1, 2 };
vector t = p;
std::cout << t << std::endl;
return 0;
}
Or how about:
#include
#include
#include
#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 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