Thread overview
compare struct with string member is wield;
Oct 12, 2011
Cheng Wei
Oct 12, 2011
Jonathan M Davis
Oct 12, 2011
Cheng Wei
Oct 12, 2011
Jonathan M Davis
Oct 12, 2011
Cheng Wei
Oct 12, 2011
Dmitry Olshansky
October 12, 2011
struct S {
  string str;
};

S g_s;

unittest {
    S s1;
    S s2;
    assert(s1 == s2);         // Success
    assert(g_s == s1);        // Failed
}

Is this expected? If so, may I know the reason? Thanks.
October 12, 2011
On Wednesday, October 12, 2011 05:16:40 Cheng Wei wrote:
> struct S {
>   string str;
> };
> 
> S g_s;
> 
> unittest {
>     S s1;
>     S s2;
>     assert(s1 == s2);         // Success
>     assert(g_s == s1);        // Failed
> }
> 
> Is this expected? If so, may I know the reason? Thanks.

It succeeds on my box (Linux 64) with the latest from git. I assume that you're using 2.055? Maybe it's a bug that was fixed since the release.

- Jonathan M Davis
October 12, 2011
Sorry. The previous is not the one causes the problem.
Try this:

struct S {
    string str = "Hello";   // Adding an initial value here.
};

S g_s;

unittest {
    S s1;
    S s2;
    assert(s1 == s2); // Success
    assert(g_s == s1); // Fail
    auto s3 = g_s;
    assert(s3 == g_s);; // Even this will fail.
}

It seems if the string is initialized with a default value, then this does not work.
October 12, 2011
On Wednesday, October 12, 2011 06:07:38 Cheng Wei wrote:
> Sorry. The previous is not the one causes the problem.
> Try this:
> 
> struct S {
>     string str = "Hello";   // Adding an initial value here.
> };
> 
> S g_s;
> 
> unittest {
>     S s1;
>     S s2;
>     assert(s1 == s2); // Success
>     assert(g_s == s1); // Fail
>     auto s3 = g_s;
>     assert(s3 == g_s);; // Even this will fail.
> }
> 
> It seems if the string is initialized with a default value, then this does not work.

Yeah. It's failing for me too. It's obviously a bug of some kind. I'd have to going digging through d.puremagic.com/issues to see whether anything like it has been reported though.

By the way, the semicolon at the end of the definition of S is unnecessary in D.

- Jonathan M Davis
October 12, 2011
Thanks. Removing the ';' after struct and class is really helpful. The ";" keeps trapping me in C++ coding. :)
October 12, 2011
On 12.10.2011 10:14, Jonathan M Davis wrote:
> On Wednesday, October 12, 2011 06:07:38 Cheng Wei wrote:
>> Sorry. The previous is not the one causes the problem.
>> Try this:
>>
>> struct S {
>>      string str = "Hello";   // Adding an initial value here.
>> };
>>
>> S g_s;
>>
>> unittest {
>>      S s1;
>>      S s2;
>>      assert(s1 == s2); // Success
>>      assert(g_s == s1); // Fail
>>      auto s3 = g_s;
>>      assert(s3 == g_s);; // Even this will fail.
>> }
>>
>> It seems if the string is initialized with a default value, then this
>> does not work.
>
> Yeah. It's failing for me too. It's obviously a bug of some kind. I'd have to
> going digging through d.puremagic.com/issues to see whether anything like it
> has been reported though.
>
> By the way, the semicolon at the end of the definition of S is unnecessary in
> D.
>
> - Jonathan M Davis

It is strange. The reason could be: by default for structs it does bitlevel memcmp style comparison. Now in this case it will compare pointer-length pairs directly, if somehow global var got different string (address-wise) then it won't compare equal to S.init no matter what.

So two things:
- print out .str.ptr  for all of you vars to see if that's the case
- define sane opEquals*:
	bool opEquals(in S s)const{
		return str == t.str;
	}

* I haven't followed the timeline of the changes to opEquals that ultimately made things much simpler so you may have to rewrite it as bool opEquals(const ref S s)const in dmd 2.055.

-- 
Dmitry Olshansky