Search code examples
c++operatorslogical-operatorsunary-operator

Is there a "normal" unary logical operator in C++


I mean, we all know that there is the negation logical operator !, and it can be used like this:

class Foo
{
public:
    bool operator!() { /* implementation */ }
};

int main()
{
    Foo f;
    if (!f)
        // Do Something
}

Is there any operator that allows this:

if (f)
    // Do Something

I know it might not be important, but just wondering!


Solution

  • You can declare and define operator bool() for implicit conversion to bool, if you're careful.

    Or write:

    if (!!f)
       // Do something