[cfe-dev] Specializing std::swap
james.dennett at gmail.com
Wed May 22 20:13:57 PDT 2013
[cfe-dev now on BCC.]
On Wed, May 22, 2013 at 8:03 PM, Aleksandar Fabijanic <aleskx at gmail.com>wrote:
> Thanks for prompt response.
> On Wed, May 22, 2013 at 9:47 PM, James Dennett <james.dennett at gmail.com>
> > I'm not sure this is topical for cfe-dev, as it's just a question about
> 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?
Sorry: for function templates. (Of course we do have partial
specialization for class templates.)
> > 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:
The _answer_ is not just about full specialization though.
> > You could write an
> > overload, except that it's not permitted to add overloads to namespace
> I am aware of that. That's why it is specialized.
You misread me. The sentence above was about class templates, e.g.,
MyContainer<T>. You cannot specialize std::swap for MyContainer<T>,
because of the lack of function partial specialization. You could
specialize for particular instances of MyContainer<T>, but that's not what
> > 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
> > enabled).
> I understand there is a workaround,
I would not call it a workaround. Calling std::swap directly is the wrong
thing to do. Adding it to an overload set along with functions (and
function templates) found via ADL is the right way to call swap.
> but if someone explicitly calls
> std::swap<Statement>, then my swap will not be called.
Right. That's why they should not do that. That's not swap's interface.
> I'm interested in a generic answer to the question: why is
> specializing standard templates (such as e.g. std::swap) not a good
Because it's not a general solution (because it can't work for class
templates), and a general solution exists (put the swap in the right
namespace, and call it via ADL).
How/where would you implement a swap for MyContainer<T>? You can't put a
function template in namespace std.
It would be ugly to have one solution for class templates and another for
non-templates. Too ugly even for C++.
> Again, thanks and if there is a better place for this discussion
> please point me there.
comp.lang.c++.moderated, Stack Overflow, choose your poison.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the cfe-dev