public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Asymmetry of user-difined swap and distance
@ 2009-08-07 18:01 Maxim Dementiev
  2009-08-07 19:11 ` Jonathan Wakely
  0 siblings, 1 reply; 3+ messages in thread
From: Maxim Dementiev @ 2009-08-07 18:01 UTC (permalink / raw)
  To: libstdc++, gcc

Hi,

Let's have a look at std::pair<>::swap and std::lower_bound<> implementations.

1. pair::swap in http://gcc.gnu.org/viewcvs/trunk/libstdc%2B%2B-v3/include/bits/stl_pair.h?view=markup

       void
       swap(pair& __p)
       {
         using std::swap;
         swap(first, __p.first);
         swap(second, __p.second);
       }

2. lower_bound in http://gcc.gnu.org/viewcvs/trunk/libstdc%2B%2B-v3/include/bits/stl_algo.h?view=markup

       _DistanceType __len = std::distance(__first, __last);

Now we need to use user-defined versions of swap and distance for corresponding types when we work with STL.

It means that swap for user types could be defined either in std namespace or in the user type namespace (argument-dependent name 
lookup).

On the other hand, distance (advance, etc.) for user types must be defined in std namespace.

How come? Why this asymmetry?

Should we always extend std namespace?

Regards,
Maxim P. Dementiev

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Asymmetry of user-difined swap and distance
  2009-08-07 18:01 Asymmetry of user-difined swap and distance Maxim Dementiev
@ 2009-08-07 19:11 ` Jonathan Wakely
  2009-08-07 19:22   ` Maxim Dementiev
  0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Wakely @ 2009-08-07 19:11 UTC (permalink / raw)
  To: Maxim Dementiev; +Cc: libstdc++, gcc

2009/8/7 Maxim Dementiev:
>
> It means that swap for user types could be defined either in std namespace
> or in the user type namespace (argument-dependent name lookup).

Yes, this is intentional. swap() is a point of customisation point,
see http://www.ddj.com/cpp/184401876 and the definition in
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1691.html

> On the other hand, distance (advance, etc.) for user types must be defined
> in std namespace.

There is no need to customise std::distance(), or certainly a lot less
need, because the Standard Library can provide optimal implementations
for all possible iterator categories.  The generic swap() function in
namespace std won't even work with some types and is sub-optimal for
others.

Jonathan

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Asymmetry of user-difined swap and distance
  2009-08-07 19:11 ` Jonathan Wakely
@ 2009-08-07 19:22   ` Maxim Dementiev
  0 siblings, 0 replies; 3+ messages in thread
From: Maxim Dementiev @ 2009-08-07 19:22 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: libstdc++, gcc

On Fri, 7 Aug 2009 19:19:03 +0100
  Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
> 2009/8/7 Maxim Dementiev:
>>
>> It means that swap for user types could be defined either in std namespace
>> or in the user type namespace (argument-dependent name lookup).
> 
> Yes, this is intentional. swap() is a point of customisation point,
> see http://www.ddj.com/cpp/184401876 and the definition in
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1691.html

Ok. Thanks!

>> On the other hand, distance (advance, etc.) for user types must be defined
>> in std namespace.
> 
> There is no need to customise std::distance(), or certainly a lot less
> need, because the Standard Library can provide optimal implementations
> for all possible iterator categories.  The generic swap() function in

Dear Jonathan, what about structures like B-Trees where the speed is equal to random iterators on one node but is lesser when you 
iterates though different nodes?
So, for me the question isn't "Do I really need it?"
But "Shall I extend the std namespace?"

Yours faithfully,
Max

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-08-07 19:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-08-07 18:01 Asymmetry of user-difined swap and distance Maxim Dementiev
2009-08-07 19:11 ` Jonathan Wakely
2009-08-07 19:22   ` Maxim Dementiev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).