[RFA] Removing const from std::swap
Gabriel Dos Reis
gdr@integrable-solutions.net
Thu May 19 17:14:00 GMT 2005
Theodore Papadopoulo <Theodore.Papadopoulo@sophia.inria.fr> writes:
| 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.
When I asked the question -- I was not actually thinking specifically
of optimizations, but of program meaning. Put differently, does it
mean to swap and why shall we put a const on the temporary?
| 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 ?
I do not understand your question. If you define swap in terms of
assignment (which the Standard currently does, but is questionable),
why would you not allow assignment on non-const argument then (since
the two operands are going to be modified) ?
-- Gaby
More information about the Libstdc++
mailing list