Why do several of the standard operators not have standard functors?

后端 未结 6 1067
庸人自扰
庸人自扰 2021-02-05 05:15

We have:

  • std::plus (+)
  • std::minus (-)
  • std::multiplies (*)
6条回答
  •  生来不讨喜
    2021-02-05 06:14

    new does not have a functor per say, but the default allocator does simply pass along the request to new. This also covers delete, since the two are linked.

    From there, I don't think the functors are really meant to be considered "things you pass to for_each, but rather "things you might need to specialize on a case by case basis."

    Removing new [and family] from the list, and you basically have a bunch of operations that have no real meaning except as specified by the language. If you take an object's address, there's really only one thing you want to happen: you get given that object's address. How might change, but what doesn't. So there's never really a need to specialize that behavior via a standard functor; you can just use & and trust operator overloading to do its thing. But the meaning of "add" or "compare" might change over the course of a program, so providing a means to do so has some merit.

    This also includes the compound assignment operators; their meaning is linked to their two pieces, so if you need std::add_assign you can just fall back on std::add [and operator =, which is absent from your list].

    The bitwise operators kind of fall in between; I could see the argument either way for them.

提交回复
热议问题