What is the meaning of operator bool() const
Member functions of the form
operator TypeName()
are conversion operators. They allow objects of the class type to be used as if they were of type TypeName
and when they are, they are converted to TypeName
using the conversion function.
In this particular case, operator bool()
allows an object of the class type to be used as if it were a bool
. For example, if you have an object of the class type named obj
, you can use it as
if (obj)
This will call the operator bool()
, return the result, and use the result as the condition of the if
.
It should be noted that operator bool()
is A Very Bad Idea and you should really never use it. For a detailed explanation as to why it is bad and for the solution to the problem, see "The Safe Bool Idiom."
(C++0x, the forthcoming revision of the C++ Standard, adds support for explicit conversion operators. These will allow you to write a safe explicit operator bool()
that works correctly without having to jump through the hoops of implementing the Safe Bool Idiom.)
bool operator in c++
TL;DR: The code inside the function is evaluating if *this
is <
rhs
, bool
is merely the return type.
The operator is operator <
which is the less than operator. The current object is considered the left hand side or lhs
, and the object compared against, the right hand of the a < b
expression is rhs
.
bool // return type
operator < // the operator
(const TriIndex& rhs) // the parameter
{
...
}
It returns true
if the current object is less than
(should preceed in containers, etc) the object after the <
in an expression like:
if (a < b)
which expands to
if ( a.operator<(b) )
There is a bool operator:
operator bool () const { ... }
which is expected to determine whether the object should evaluate as true:
struct MaybeEven {
int _i;
MaybeEven(int i_) : _i(i_) {}
operator bool () const { return (_i & 1) == 0; }
};
int main() {
MaybeEven first(3), second(4);
if (first) // if ( first.operator bool() )
std::cout << "first is even\n";
if (second) // if ( second.operator bool() )
std::cout << "second is even\n";
}
Why does operator char*() override operator bool() const in boolean context?
As explained by @oakad — the elegant solution is to use explicit operator char*()
in c++11 mode. This will ensure operator char*()
simply doesn't get used in boolean context.
As explained by @bolov — the observed behavior is part of the language standard. According to 13.3.1, for the purpose of resolving overloaded functions, methods are considered to have an implicit object parameter. For methods declared without a ref-qualifier, this parameter is a reference to the class with the corresponding cv-qualifier.
So we effectively have the following situation:
char* op(A& a) { return a.buf; }
bool op(const A& a) { return !a.err; }
Since a
was non-const
in main()
, the non-const
operator gets picked. It doesn't matter whether we had operator char*()
or operator int()
or operator double()
— a non-const
cast operator which could be used in boolean context would have higher precedence than operator bool() const
.
Why C++ 11 added operator bool to the ios classes
In C++98 there were no explicit
cast operators so if you had an operator bool
it meant that the object could be used as a bool
or anything that can be cast from bool
(such as int
) this meant that you could accidentally use your objects in ways that you wouldn't expect or want (such as obj + 2
). Some objects provided a cast to void*
which meant that the object could be tested in an if
statement (not null) but would not be passable to functions expecting int
etc.
With the introduction of explicit
cast operators this is no longer needed and in order to have a testable object it's much better to use explicit operator bool
than operator void*
.
Related Topics
Why Did Microsoft Abandon Long Double Data Type
What Does the Operation C=A+++B Mean
Casting Between Void * and a Pointer to Member Function
Stack-Buffer Based Stl Allocator
How to Have Multiple Parameter Packs in a Variadic Template
Does the 'Offsetof' MACro from <Stddef.H> Invoke Undefined Behaviour
Cpp - Valgrind - Invalid Read of Size 8
Difference Between C++03 Throw() Specifier C++11 Noexcept
How to Use Standard Library (Stl) Classes in My Dll Interface or Abi
How Can a C++ Header File Include Implementation
How to Know Which Parts in the Code Are Never Used