adding function overload to std - Good or Bad?

D

Daniel Pitts

I've created a class with semantics such that it makes sense to call
std::abs(foo) and get a (double) result.

Is it sensible to add to the std namespace, or would it be better to put
an "abs" function it in the same namespace as my class was created?
 
S

sumsin

I've created a class with semantics such that it makes sense to call
std::abs(foo) and get a (double) result.

Is it sensible to add to the std namespace, or would it be better to put
an "abs" function it in the same namespace as my class was created?

No it wouldn't be good.
 
S

sumsin

Thanks for the ambiguous response, it's very helpful.

Which wouldn't be good, and more importantly, *why*?

because std namespace is meant for standard utilities and your
semantics for 'abs' function is not standard from the language
perspective thats why i am suggesting no. So keep it into your custom
namespace.
 
T

Tomás Ó hÉilidhe

because std namespace is meant for standard utilities and your
semantics for 'abs' function is not standard from the language
perspective thats why i am suggesting no. So keep it into your custom
namespace.

Or you could do:

namespace super_std {

using namespace std;

/* Put more stuff here */
}
 
R

red floyd

Rolf said:
Putting it into namespace std.


One reason would be because the C++ standard forbids it.

I thought it was OK to add template specializations in std::
 
R

Rolf Magnus

red floyd wrote:

Isn't std::abs a template?

Well, there is an std::abs template, but only for std::valarray:

template<class T> valarray<T> abs (const valarray<T>&);

The rest are just overloads.
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top