Topic: Defect Report: std::pair allows impl conv of pair of expl convs
Author: kprateek88@yahoo.com (Prateek R Karandikar)
Date: Fri, 11 Jun 2004 22:01:38 +0000 (UTC) Raw View
20.2.2 Pairs [lib.pairs] paragraph 4
<quote>
template<class T1, class T2>
struct pair{/*...*/};
...
template<class U, class V> pair(const pair<U, V> &p);
Effects: Initializes members from the corresponding members of the
argument, performing implicit conversions as needed.
</quote>
(Here, for brevity, I assume:
typedef std::vector<int> Vec;
typedef std::vector<int>::size_type S;
)
Statment 1:This allows a pair<A,B> to be *implicitly* converted to a
pair<C,D>, even if the A to B and C to D conversions are only
*explicit* and not implicit.
In the Effects clause, the word "initializes" does not prevent
explicit conversion. For example, we can *initialize* a Vec from an S,
even though the Vec::Vec(S) constructor is explicit.
So, had the Effects clause just said "Initializes members from the
corresponding members", Statement 1 would have been true. Now, adding
the phrase "performing implicit conversions as needed" does not
disallow performing explicit conversions, and so does not falsify
Statement 1.
So, Statement 1 is true, which I doubt was the intention.
Proposed Resolution:
Add:
Requires: U can be implicitly converted to T1. V can be implicitly
converted to T2.
Remove the phrase "performing implicit conversions as needed".
Add:
[Note: This can be implemented as
template <class T> void check(const T&){} //non-member
template<class T1, class T2>
template<class U,class V> pair<T1,T2>::pair(const pair<U,V> &p) :
first((check<T1>(p.first),p.first)),
second((check<T2>(p.second),p.second)) {}
-end note]
Add a constructor:
template <class U, class V>
explicit pair(const pair<U,V> &p) : first(p.first), second(p.second)
Effects: As if implemented above
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]