[cfe-dev] Specializing std::swap

Aleksandar Fabijanic aleskx at gmail.com
Wed May 22 20:03:33 PDT 2013


Thanks for prompt response.

On Wed, May 22, 2013 at 9:47 PM, James Dennett <james.dennett at gmail.com> wrote:

> I'm not sure this is topical for cfe-dev, as it's just a question about C++.

I'm not sure, either and I apologize if this is out of place. If
someone can point me to the right place (or continue private
discussion) I'd be happy to do that. I just never had problem with
other compilers, so I thought I'd ask here for clarification.

> C++ doesn't have partial specialization for templates,

I guess you meant for functions?

> so it's not possible
> to define a std::swap for a user-define template.

Poco::Data::Statement is not a template.

The question is about full specialization, which is explicitly permitted:

17.4.3.1 Reserved names [lib.reserved.names]
1 ... A program may add template specializations for any standard
library template to namespace std. Such a specialization (complete or
partial) of a standard library template results in undefined behavior
unless the declaration depends on a user-defined type of external
linkage and unless the specialization meets the standard library
requirements for the original template.
...

> You could write an
> overload, except that it's not permitted to add overloads to namespace std.

I am aware of that. That's why it is specialized.

> On the bright side, ADL means that it's not necessary to put those in
> namespace std anyway, so long as people use swap properly (such that ADL is
> enabled).

I understand there is a workaround, but if someone explicitly calls
std::swap<Statement>, then my swap will not be called.

I'm interested in a generic answer to the question: why is
specializing standard templates (such as e.g. std::swap) not a good
practice?

Again, thanks and if there is a better place for this discussion
please point me there.

Alex



More information about the cfe-dev mailing list