The C++1x standard has deprecated the old STL binder functions in favor of the more universal std::bind
. However, it seems that std::not1
and std::not2
are not deprecated in favor of a universal std::not_
or something. The reality is that the <functional>
part of the STL, prior to C++1x, was really cumbersome to work with due to 1) the lack of lambdas, 2) the fact that the binders and negation functors required a nested typedef argument_type
, meaning they couldn't work with ordinary functions, and 3) the lack of variadic templates necessitated separate binder and negation functions depending on the number of arguments.
C++1x changed all of this, dramatically improving the usefulness of <functional>
. But for some reason, C++1x seems to have improved everything except std::not1
and std::not2
. Really, it would be nice to have a standard universal negate
function, like:
template <class F>
class negation
{
public:
neg开发者_如何转开发ation(F f) : m_functor(f) { }
template <class... Args>
bool operator() (Args&&... args) const
{
return (!m_functor(args...));
}
private:
F m_functor;
};
template <class F>
inline negation<F> not_(F f)
{
return negation<F>(f);
}
This would of course deprecate std::not1
and std::not2
in the same way the old binders were deprecated.
Question(s): 1) I've looked through the C++1x draft, and don't see any mention of a universal negate
function. Did I miss it? 2) Is there some compelling reason why they added a universal bind
and deprecated the old binders, but failed to do the same for the negation functions?
You didn't miss it.
Compelling reason? It depends on who you ask. The lack of this functionality was discussed, but not until very late in the process. Hmm... I can't find the paperwork on that at the moment, there may not be any.
The best solution for this (imho) is to add operator!() to bind. But by the time this came up, the committee was in no mood to add new features to C++11. Perhaps it will come in a technical report.
Oh, here's the paperwork:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3224.html#gb97
Effectively, if not officially, all of the old function object binding system is deprecated, since lambdas are a much superior solution. I find it more curious that they bothered to update bind
and any of the rest of it at all.
Most likely, you will find that this is because the original boost::bind
did not provide such a negate
function, and all of the new TR1/C++0x binding mechanism is based on that, and nobody noticed that not
was missing.
精彩评论