|
Boost : |
From: Howard Hinnant (hinnant_at_[hidden])
Date: 2005-02-14 11:06:56
On Feb 14, 2005, at 7:06 AM, John Maddock wrote:
> The following simple patch to tuple_basic.hpp allows tuples to be
> constructed from std::pairs (there is already an assignment operator,
> but the copy constructor is missing for some reason, and is required
> for TR1 conformity).
>
> RCS file: /cvsroot/boost/boost/boost/tuple/detail/tuple_basic.hpp,v
> retrieving revision 1.28
> diff -r1.28 tuple_basic.hpp
> 627a628,636
>> template <class T, class U>
>> tuple(const std::pair<T, U>& p)
>> : inherited(p.first, p.second, detail::cnull(), detail::cnull(),
>> detail::cnull(), detail::cnull(), detail::cnull(),
>> detail::cnull(), detail::cnull(), detail::cnull())
>> {
>> BOOST_STATIC_ASSERT(length<tuple>::value == 2);// check_length = 2
>> }
>>
>
> Is it acceptable to add this?
I think we could do better, but it would be stretching TR1 (which I
think is good).
I think any compatible tuple-like type should be convertible to tuple
(pair, array, maybe complex?, anything else that declares itself
tuple-like and implements the tuple interface). Furthermore, I think
is_convertible should answer correctly, even for negative cases (for
those pair that are not convertible to a 2-tuple).
E.g.:
pair<short, long> p(0, 1);
tuple<long, short> t1(p);
array<int, 2> a = {1, 2};
tuple<long, short> t2(a);
static_assert(is_convertible<array<int, 2>, tuple<long, short> >::value,
"array<int, 2> is not convertible to tuple<long, short>");
static_assert(is_convertible<pair<short, long>, tuple<long, short>
>::value,
"pair<short, long> is not convertible to tuple<long,
short>");
static_assert(!is_convertible<pair<short*, long>, tuple<long, short>
>::value,
"pair<short*, long> is convertible to tuple<long,
short>");
Such a converting constructor can be done generically, instead of once
for each tuple-like type, if we bring back the (sorely missed)
is_tuple_like trait. Actually, now that I think more about it, I'm not
positive is_tuple_like is really needed (but my proof of concept
currently uses it).
template <class P>
tuple(const P& p, typename
Metrowerks::restrict_to<__tuple_convertible<P, tuple>::value>::type* =
0);
Where __tuple_convertible does a size check, and an element by element
is_convertible check.
-Howard
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk