user3735658
user3735658

Reputation:

Overloading typecast to bool and efficiency

Say I have a class C that I want to be able to implicitly cast to bool to use in if statements.

class C {
public:
    ...
    operator bool() { return data ? true : false; }
private:
    void * data;
};

and

C c;
...
if (c) ...

But the cast operator has a conditional which is technically overhead (even if relatively insignificant). If data was public I could do if (c.data) instead which is entirely possible and does not involve any conditionals. I doubt that the compiler will do any implicit conversion involving a conditional in the latter scenario, since it will likely generate a "jump if zero" or "jump if not zero" which doesn't really need any Boolean value, which the CPU will most likely have no notion of anyway.

My question is whether the typecast operator overload will indeed be less efficient than directly using the data member.

Note that I did establish that if the typecast directly returns data it also works, probably using the same type of implicit (hypothetical and not really happening in practice) conversion that would be used in the case of if (c.data).

Edit: Just to clarify, the point of the matter is actually a bit hypothetical. The dilemma is that Boolean is itself a hypothetical construct (which didn't initially exist in C/C++), in reality it is just integers. As I mentioned, the typecast can directly return data or use != instead, but it is really not very readable, but even that is not the issue. I don't really know how to word it to make sense of it better, the C class has a void * that is an integer, the CPU has conditional jumps which use integers, the issue is that abiding to the hypothetical Boolean construct that sits in the middle mandates the extra conditional. Dunno if that "clarification" made things any more clear though...

Upvotes: 0

Views: 234

Answers (3)

Anton Savin
Anton Savin

Reputation: 41321

On modern compilers these two functions produce the same machine code:

bool toBool1(void* ptr) {
    return ptr ? true : false;
}

bool toBool2(void* ptr) {
    return ptr;
}

Demo

So it really doesn't matter.

Upvotes: 1

Mike DeSimone
Mike DeSimone

Reputation: 42825

It depends on how smart your compiler's optimizer is. I think they should be smart enough to remove the useless ? true: false operation, because the typecast operation should be inlined.

Or you could just write this and not worry about it:

operator bool() { return data; }

Since there's a built-in implicit typecast from void* to bool, data gets typecast on the way out the function.

I don't remember if the conditional in if expects bool or void*; at one point, before C++ added bool, it was the latter. (operator! in the iostream classes returned void* back then.)

Upvotes: 3

Tony Delroy
Tony Delroy

Reputation: 106216

My question is whether the typecast operator overload will indeed be less efficient than directly using the data member.

Only examining your compiler output - with the specific optimisation flags you'd like to use - can tell you for sure, and then it might change after some seemingly irrelevant change like adding an extra variable somewhere in the calling context, or perhaps with the next compiler release etc....

More generally, C++ wouldn't be renowned for speed if the optimisers didn't tend to handle this kind of situation perfectly, so your odds are very good.

Further, write working code then profile it and you'll learn a lot more about what performance problems are actually significant.

Upvotes: 3

Related Questions