-3

I'm stuck on evaluating a struct as a bool in c++

struct foo {
  int bar;
};
foo* x = (foo*)malloc(sizeof(foo));
int y = ( x ) ? 3 : 4;

This is what happens when it is ran:

could not convert 'x' from 'foo' to 'bool'

When searching for bool operators, I can only find ways to overload the comparator bool operator. Is there a straight up evaluate struct as a bool operator?

I also want to keep foo as plain old data if possible.

Gideon
  • 1,203
  • 1
  • 11
  • 13

2 Answers2

6

There is no straight-up evaluate-struct-as-a-bool operator. That's why there's a way to give a struct an operator bool method. It tells the compiler how to interpret a value of the given struct type when it's used in a bool context.

The code you've shown would not use such an operator, though, since the operator applies to structs, not pointers to structs; you have an expression of type foo*, not foo. Pointers automatically convert to bool because that's an intrinsic conversion the compiler knows how to perform. Null pointers are false, and non-null pointers are true. The code shown in the question would never yield the reported error message.

If you do indeed have a struct in a place where the compiler expects a bool, and you cannot, for whatever reason, give the struct an operator bool method, then you can perform the conversion the old-fashioned way and use an ordinary function:

bool interpret_foo_as_bool(foo const& f);

Implement the function however you want, and then call it, passing your foo value. For example:

foo* x = ...;
int y = interpret_foo_as_bool(*x) ? 3 : 4;
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
6

This can't be the correct code for that error. The error states that it can't convert x from foo to bool, but x is not a foo, it is a foo*. And a foo* is convertable to bool1, so there would be no error for the code you have given.

If you want to be able to use a foo object as a conditional, you need to provide operator bool member function for your foo class:

struct foo {
  int bar;
  operator bool() { return true; }
};
foo x;
int y = x ? 3 : 4;

In this case, any foo will always evaluate to true. This conversion to bool is considered "unsafe" because your object may be converted to bool in some unexpected places. To get around this, in C++03 you can use the safe bool idiom and in C++11 you can mark your operator bool as explicit:

struct foo {
  int bar;
  explicit operator bool() { return true; }
};

The explicit operator bool will still be used automatically in certain places, such as as a condition in if statements and while loops. There are other answers that cover this.


1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.

Community
  • 1
  • 1
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • You should probably also mention why it is generally really bad practice to implement the conversion-to-bool operation using T::operator bool() as well. – mauve Dec 04 '12 at 21:44
  • Thank you, I didn't realize that defining an operator on a struct would preserve it's POD status. – Gideon Dec 04 '12 at 21:47