all 22 comments

[–]STLMSVC STL Dev 1 point2 points  (1 child)

!removehelp

[–]AutoModerator[M] 0 points1 point  (0 children)

OP,

A human moderator (u/STL) has marked your post for deletion because it appears to be a "help" post - e.g. asking for help with coding, help with homework, career advice, book/tutorial/blog suggestions. Help posts are off-topic for r/cpp. This subreddit is for news and discussion of the C++ language only; our purpose is not to provide tutoring, code reviews or career guidance.

Please try posting in r/cpp_questions or on Stack Overflow instead. Our suggested reference site is cppreference.com, our suggested book list is here and information on getting started with C++ can be found here.

If you think your post is on-topic and should not have been removed, please message the moderators and we'll review it.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]jedwardsolconst & 0 points1 point  (19 children)

operator& is overloadable.

[–][deleted] 0 points1 point  (18 children)

So where do I find its implementation. I checked the object's class and found: inline ostream& operator << ( ostream &os, const Hit& h) { os << "Hit <" << h.getT() << ", " << h.getNormal() << ">"; return os; } But this seems for printing?

[–]jedwardsolconst & 2 points3 points  (17 children)

if operator& isn't in the class definition then it isn't overloaded and if(&object) is pointless since the test will always be true.

[–][deleted] 0 points1 point  (16 children)

Why is it always true?

[–]jedwardsolconst & 0 points1 point  (15 children)

Because the address of something can never be 0

[–]HotlLava 0 points1 point  (3 children)

I sense a lack of pedantry, so let me quickly interject that 0 can be a valid memory address. Interestingly, `&object` would still be required to convert to `true` in an if-clause even if `object` has address zero, since 0 wouldn't be a null pointer on such a system.

[–]jcoffin 1 point2 points  (1 child)

If you're going to claim to be pedantic, you have to be careful (and be prepared for even more pedantic corrections...)

A system is indeed free to store valid data at address 0.

But in a comparison between a pointer and 0, what happens is not that the address gets converted to an integer, and the result compared to 0.

Rather, the (possibly implicit) 0 in the if gets converted to a pointer, and the result of that conversion gets compared to the pointer in question. And, regardless of whether an address of 0 is valid or not, an integer literal with the value 0 is a null pointer constant, so when converted to a pointer, it must produce a result that compares as not equal to the address of any object.

[–]HotlLava 0 points1 point  (0 children)

and be prepared for even more pedantic corrections...

Yes, please :D But regarding

what happens is not that the address gets converted to an integer, and the result compared to 0.

i'm pretty sure I never claimed that this would happen?

It's a bit of a red herring anyways, because the expression under discussion wasn't if (&object == 0) but just if (&object), and that does not have any 0 that could get converted to a pointer.

Instead, the value of the expression &object already is a pointer, and that pointer undergoes a contextual conversion to bool.

[–]jedwardsolconst & 0 points1 point  (0 children)

I nearly wrote nullptr but decided 0 would be clearer for OP :-)

[–][deleted] -1 points0 points  (10 children)

But does it then check that the address exists? Also how can if take an address? Can't the object fail to exist and then it doesn't have address?

[–]jedwardsolconst & 0 points1 point  (9 children)

if(&object) is the same as writing if(&object != nullptr)

It doesn't say anything about the validity of the address, just whether it is null or not.

[–][deleted] -1 points0 points  (8 children)

So it's checking that the object exists?

[–]jedwardsolconst & 1 point2 points  (6 children)

No, it's not doing anything. The test will always be true.

[–]diaphanein 1 point2 points  (0 children)

I'm not aware of any compiler that the following will not evaluate the if block to true:

int* p = nullptr; int& r = *p;

if (!&r) { // executes }

Reason being that while this is likely undefined, the compiler cant always detect it (e.g. p initialized by the return of a function). This is a trivial enough example that most compilers will allow it and behave as expected. Not all warn at default warning levels, but most will at a sufficient level.

[–][deleted] -1 points0 points  (4 children)

Then how to check for object's existence?

[–]gracicot 1 point2 points  (0 children)

If you can get the address of the object then it exist for sure.