Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unary and Binary Negation

Tags:

c++

c++11

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:

    negation(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?

like image 550
Channel72 Avatar asked Apr 06 '11 13:04

Channel72


People also ask

Is the negation operator unary or binary?

The unary negation operator (-) produces the negative of its operand. The operand to the unary negation operator must be an arithmetic type.

What is negation in binary?

The conversion of a binary digit, or bit, into its opposite. For example, a 1 converted into a 0 is considered a negation. A complete negation of an 8-bit binary number may look similar to the example below. Binary Number: 01100101. Negation: 10011010.

What is the difference between unary and binary?

There are two types of mathematical operators: unary and binary. Unary operators perform an action with a single operand. Binary operators perform actions with two operands.

What is unary negation in Python?

Negation: The not operator in Python can be used only in the unary form, which means negation, returning the a result that is the opposite of its operand. Its boolean prototype is not (bool) -> bool.


2 Answers

  1. You didn't miss it.

  2. 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

like image 195
Howard Hinnant Avatar answered Oct 01 '22 22:10

Howard Hinnant


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.

like image 42
Puppy Avatar answered Oct 01 '22 22:10

Puppy