Placing an overload of an std:: algorithm/utility function in the std namespace

S

Shriramana Sharma

Hello. IIUC one is normally discouraged from trying to include one's own code into the std:: namespace, but it is OK to do so when one is specializinga generic std:: algorithm or utility function (like swap) for one's own classes, right? I mean one would then do:

namespace std {
void swap ( myclass & a, myclass & b ) { _code_goes_here_ }
}

.... correct?

Thank you!
 
B

Bo Persson

Shriramana Sharma wrote 2013-06-27 11:09:
Hello. IIUC one is normally discouraged from trying to include one's own code into
the std:: namespace, but it is OK to do so when one is specializing a
generic std:: algorithm or utility function (like swap) for
one's own classes, right?

I mean one would then do:

namespace std {
void swap ( myclass & a, myclass & b ) { _code_goes_here_ }
}

... correct?


Technically this is an overloaded function and not a specialization, so
not allowed.

In practice, you should add your swap function to the same namespace as
myclass and the standard library will pick it up using argument
dependent lookup.


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top