This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [RFA] Removing const from std::swap


On Thu, 2005-05-19 at 19:18 +0200, Gabriel Dos Reis wrote:
> Paolo Carlini <pcarlini@suse.de> writes:
> 
> | Hi everyone,
> | 
> | some time ago, in an overzealous (it seems ;) series of const- tweaks to
> | the algorithms, I touched std::swap too, adding a const to the temporary
> | __tmp:
> | 
> |   template<typename _Tp>
> |     inline void
> |     swap(_Tp& __a, _Tp& __b)
> |     {
> |       // concept requirements
> |       __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)
> | 
> |       const _Tp __tmp = __a;
> |       __a = __b;
> |       __b = __tmp;
> |     }
> | 
> | This is strictly speaking absolutely legal, as the concept check also
> | demonstrates, but now people on the c++.moderated list, Howard Hinnant
> | for example, believe that this is "overly pedantic" ;) ... Shall I
> | remove it? 
> 
> I'll look at it differently: Does the const buy us anything?  If not,
> it should be removed.

The mere fact that it introduces a difference in the behaviour of swap
means that there is some concept hidden behind adding a const or not
adding it (as noted by Martin).

const is not only something that allows for better optimization, it is
also some semantic concept that is (in theory) meaningful for better
expressing the constraints on the program.

That being said, Martin's argument on why dropping the const right now
seems convincing.... On the other hand, I do not understand why an
assignment operator could be allowed to use a non-const argument (well
of course reference counting comes to mind, but in that case I'd argue
that there is missing mutable keyword somewhere ???) Maybe the standard
is too flexible in allowing that ?
Anyone knows the rationale behind such design ?


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]