Is there any reason for std::multiplies and std::divides to be in third person? [closed]
Asked Answered
F

1

17

Today we discovered that the functors for multiplying and dividing, are called std::multiplies and std::divides, as opposed to, for example, std::multiply and std::divide respectively.

This is surprising to say the least, considering that std::plus and std::minus are not formulated the same way.

Is there a particular reason for the difference?

Filefish answered 28/3, 2014 at 20:58 Comment(10)
Does a std::add or std::subtract in any form even exist?Baldpate
Also, I made my own since the committee is currently blackhole-ing on the subject: gist.github.com/ThePhD/9842898Baldpate
Note these have not been introduced in C++14, they have been around for a long time.Peccary
@FerdinandBeyer Ah, they go under the name std::plus and std::minus, and they use void as a default argument to get the kind I put up in my gist above. How strange.Baldpate
Hmm there's an interesting remark at SGI's page: sgi.com/tech/stl/times.html (although I'm not sure if the old name is older than the StdLib name in this case).Miyamoto
If it's plus and minus, it would actually make more sense to have times and dividedby, but especially that last one just sounds ridiculous -_-Vipul
@Doorknob times has been used and given up by SGI because of a name clash with a Unix function, see the link I posted earlier.Miyamoto
Why are you surprised? They all end with an s now - all but negate, the black sheep. Much more consistent this way ;)Miyamoto
@ThePhD: The void specializations are new in C++14. This technique was chosen to avoid breaking existing code that uses these templates and to avoid having to introduce new names. Given these two design goals, the void specialization solution is pretty slick.Vanhook
@JamesMcNellis Well, in that case... typedef std::plus<> add typedef std::minus<> subtract typedef std::multiplies<> multiply typedef std::divides<> divide -- ezpz!Baldpate
C
2

It looks like this is nothing more than a blooper: plus and minus are even not verbs...

The name themselves are not C++14 originals: C++14 just adds the <void> specialization, but the typed version and all other <functional> header stuff exist from C++98 (and even pre-iso), and certain coding convention (functions as verbs, object as substatives interface as adjectives...) were not yet already well established.

What C++14 does is just add one more feature to existing definitions letting existing code to continues to works as is. It simply cannot redefine names.

That said, consider also that the + sign is not always used across the entire standard library for add: in std::strings it is concatenation, and std::plus, if applied to strings, concatenates them. Similarly, the * is often used as a "closure" operation (think to boost::spirit).

A more proper "from scratch" library will most likely call them neutrally as cross, dash, star and slash, letting the classes that provides the corresponding operations to give them consistent names in their own context

Cachou answered 28/3, 2014 at 22:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.