Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Placing an overload of an std:: algorithm/utility function in the std namespace

Reply
Thread Tools

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

 
 
Shriramana Sharma
Guest
Posts: n/a
 
      06-27-2013
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!
 
Reply With Quote
 
 
 
 
Bo Persson
Guest
Posts: n/a
 
      06-27-2013
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

 
Reply With Quote
 
 
 
 
Shriramana Sharma
Guest
Posts: n/a
 
      06-27-2013
Hi people and thanks for your replies. I understand it now.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The node.js Community is Quietly Changing the Face of Open Source Rodrick Brown Python 2 04-17-2013 04:47 PM
Re: The node.js Community is Quietly Changing the Face of Open Source Ned Batchelder Python 0 04-16-2013 04:25 PM
Is there a difference between the use of the word montage vscollage Danny D. Digital Photography 8 04-15-2013 02:24 PM
Windows 8 - so bad it's hastening the death of the PC? ~misfit~ NZ Computing 18 04-15-2013 04:15 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM



Advertisments