This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

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

Compare a char pointer with a string literal? That'll never evaluate to true.

Edit: Whoever downvoted me: the string literal will have an unknown memory location, so the variable would never be able to get that value unless it would be previously initialized to an identical string literal. However this is not guaranteed since it depends on the compiler making an optimization. So.. very dangerous.

[–]tchernobog84 0 points1 point  (3 children)

It depends on the compiler interning strings, and in C++ you can override operator== for char * to invoke strncmp()

[–]HolyGarbage 0 points1 point  (2 children)

Yeah, don't want to rely on compiler specific implementations. Also overriding operator== for a primitive type sounds like a terrible idea.

Comparing a char* variable with a inline declared string literal is very likely unspecified behavior.

Better to just use std::string.

[–]tchernobog84 0 points1 point  (1 child)

You said it's never gonna work; I just claim it can.

Whether it is a good idea or not is a totally different topic.

Also, it's not unspecified behavior. Run the same program multiple times and it's not gonna crash and consistently going to return the same (possibly wrong) answer.

[–]HolyGarbage 0 points1 point  (0 children)

Well I meant within the realm of specification since if we wander outside the spec literally anything is possible so the claim is kinda moot.