Re: emplace() overloads
Sylvain Pion ha scritto:
In the current working draft N2461, I read that associative
containers provide:
template <class... Args> pair<iterator, bool> emplace(Args&&... args);
template <class... Args> iterator emplace(const_iterator position,
Args&&... args);
What if I wish to pass a const_iterator as first argument and mean
to use the first overload?
This situation might occur only under very specific circumstances. Let's
see when:
1) if the container is a map/multimap: the value_type is
std::pair<const key_type, mapped_type>
so emplace() can only have up to two arguments. Because we know the
constructors of std::pair, if the first argument is a const_iterator,
then there must be a second argument and the const_iterator is used to
initialize an object of value key_type. Therefore key_type shall have
one constructor that can take exactly one parameter of type
const_iterator. The key type shall be complete at the point of
instantiation of the container, so such constructor can not mention the
const_iterator explicitly, but must be a template, for example like this:
template <class T>
key(T x);
(there might be other arguments with default values, but they won't make
much difference.) Now... what use can you make of a single iterator of a
type that you don't know? The only thing that comes to my mind is to
store it in some boost::any or other polymorphic container.
2) if the container is a set/multiset, then we have slightly more
freedom, because all parameters are used to initialize the value_type
which is the same as the key_type. For the same reasoning as above, the
constructor of the key_type must be a template, but now it can have two
parameters, which may constitute a range. This makes the situation more
likely to be useful, but still I can't imagine a practical use case
realizing this scenario.
Is this an oversight, or is this a deliberate choice?
What about using a different name for one of the 2 overloads, in order
to prevent the clash?
I believe this is a deliberate choice, but even if it weren't I suggest
you provide a strong and reasonable use-case where a disambiguation is
necessary before proposing a change.
HTH,
Ganesh
---
[ 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.comeaucomputing.com/csc/faq.html ]