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?

Community
  • 1
  • 1
Shoe
  • 74,840
  • 36
  • 166
  • 272
  • Does a `std::add` or `std::subtract` in any form even exist? –  Mar 28 '14 at 21:01
  • 1
    Also, I made my own since the committee is currently blackhole-ing on the subject: https://gist.github.com/ThePhD/9842898 –  Mar 28 '14 at 21:01
  • 2
    Note these have not been introduced in C++14, they have been around for a long time. – Ferdinand Beyer Mar 28 '14 at 21:03
  • @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. –  Mar 28 '14 at 21:04
  • Hmm there's an interesting remark at SGI's page: http://www.sgi.com/tech/stl/times.html (although I'm not sure if the old name is older than the StdLib name in this case). – dyp Mar 28 '14 at 21:07
  • 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 -_- – tckmn Mar 28 '14 at 21:07
  • @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. – dyp Mar 28 '14 at 21:08
  • 2
    Why are you surprised? They all end with an `s` now - all but `negate`, the black sheep. Much more consistent this way ;) – dyp Mar 28 '14 at 21:16
  • 2
    @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. – James McNellis Mar 28 '14 at 21:16
  • @JamesMcNellis Well, in that case... `typedef std::plus<> add` `typedef std::minus<> subtract` `typedef std::multiplies<> multiply` `typedef std::divides<> divide` -- ezpz! –  Mar 28 '14 at 21:25

1 Answers1

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

Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63