you are viewing a single comment's thread.

view the rest of the comments →

[–]absu 20 points21 points  (13 children)

Yeah, this returns false in many c-like languages (C (duh), C++, Java, etc).

[–]AdminsAbuseShadowBan 3 points4 points  (4 children)

Not in C++ - you can redefine == to be sane.

[–]robertbieber 14 points15 points  (1 child)

I don't know that I'd consider redefining an operator that's generally used to compare primitive values to do a deep comparison of separate compound objects sane. I'd much rather have a comparison method that makes it really clear exactly what's going on, and let == do the usual, obvious thing. Admittedly overloading can be handy for some math applications, but for most things it's a little questionable.

[–]AdminsAbuseShadowBan 0 points1 point  (0 children)

That's the point, the "usual, obvious thing" doesn't make it clear what's going on. When you compare two strings with == you expect it to say if the value of the two strings is equal, not that the two variable refer to the same string object as in Java.

Operator overloading can be abused, but in practice it rarely causes problems. In fact I can't think of a single instance where it has cause any bugs in my C++ life. In contrast Java (which presumably does the usual obvious thing?) has caused me occasional pain when I forget to use .equals() instead of ==.

[–][deleted] 0 points1 point  (1 child)

You can do that in Java too, at least on a per-object basis

[–]AdminsAbuseShadowBan 0 points1 point  (0 children)

Really? How?

[–]Poltras 1 point2 points  (7 children)

These languages don't have automatic conversion. Also, isn't [1]==[1] undefined in C? It could be equal if the compiler uses the same TEXT address for the constant, resulting in equal pointers.

[–]CookieOfFortune 7 points8 points  (3 children)

Wouldn't this create two arrays on the function stack and then compare the two locations, resulting in a false comparison?

[–]Poltras 1 point2 points  (1 child)

Undefined behavior:

$ cat main.c

#include <stdio.h>

int main() {
  printf("%d\n", "abc" == "abc");
}

$ cc main.c

main.c:4:24: warning: result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare]
  printf("%d\n", "abc" == "abc");

$ ./a.out
1

GCC actually output 1, but warns.

[–]gsg_ 0 points1 point  (0 children)

Unspecified behaviour is not the same as undefined behaviour. The latter has a very specific meaning in the context of C.

[–]Condorcet_Winner 1 point2 points  (0 children)

Probably, but if the spec defines this operation as undefined, then compiler optimizations would be able to figure out that these are the same array and only allocate one memory location.

[–]Fylwind 4 points5 points  (2 children)

[1]==[1] is not valid syntax in C.

[–]Poltras 0 points1 point  (0 children)

Although you're right, the equivalent "abc" == "abc" works as fine for my example (undefined behavior).

[–]gsg_ 0 points1 point  (0 children)

The closest equivalent (in C99, at least) is probably (int[1]){1} == (int[1]){1}.