
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.

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. IMHO, Chris

On Wed, Oct 05, 2011 at 09:07:56PM +0200, 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. --
Are you sure you're not misunderstanding what your implementation does? VC10 and GCC 4.4.5 makes (rightfully so) a 1-tuple with an element of type pair<T1,T2> when invoking make_tuple with a pair. I would be _very_ surprised if I got a 2-tuple if I make_tuple(a_pair) and would consider it a serious bug in the library. Lars Viklund | zao@acc.umu.se

Lars Viklund wrote:
Are you sure you're not misunderstanding what your implementation does? VC10 and GCC 4.4.5 makes (rightfully so) a 1-tuple with an element of type pair<T1,T2> when invoking make_tuple with a pair.
What I mean is this: #include <boost/tuple/tuple.hpp> void trigger (::std:: pair < int, int > const &p) { ::boost:: get < 0 > (::boost:: tuple < int, int > (p)) == p. first; } error: no matching function for call to ‘boost::tuples::tuple<int, int>::tuple(const std::pair<int, int>&)’ Compare: #include <tr1/tuple> void trigger (::std:: pair < int, int > const &p) { ::std:: tr1:: get < 0 > (::std:: tr1:: tuple < int, int > (p)) == p. first; } I guess this, and Joel’s comment about boost fusion, means boost tuple should be deprecated. Chris

----------------------------------------
To: boost-users@lists.boost.org From: giecrilj@stegny.2a.pl Date: Fri, 7 Oct 2011 11:18:24 +0200 Subject: Re: [Boost-users] a tuple from a pair?
Lars Viklund wrote:
Are you sure you're not misunderstanding what your implementation does? VC10 and GCC 4.4.5 makes (rightfully so) a 1-tuple with an element of type pair<T1,T2> when invoking make_tuple with a pair.
What I mean is this:
#include <boost/tuple/tuple.hpp> void trigger (::std:: pair < int, int > const &p) { ::boost:: get < 0 > (::boost:: tuple < int, int > (p)) == p. first; }
error: no matching function for call to ‘boost::tuples::tuple<int, int>::tuple(const std::pair<int, int>&)’
Compare:
#include <tr1/tuple> void trigger (::std:: pair < int, int > const &p) { ::std:: tr1:: get < 0 > (::std:: tr1:: tuple < int, int > (p)) == p. first; }
I guess this, and Joel’s comment about boost fusion, means boost tuple should be deprecated.
Chris
If you use boost::fusion::at_c<0> rather than std::get<0> or boost::get<0>, you don't even need to convert to a tuple<int, int> - it "just works" on std::pair, boost::tuple, std::tuple, or any other random-access Fusion sequence. Regards, Nate

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
participants (4)
-
Joel de Guzman
-
Krzysztof Żelechowski
-
Lars Viklund
-
Nathan Ridge