Why is b[2] false?
Asked Answered
I

1

11
string s;
bool b[] = {s=="",  s==s.c_str(),  s.c_str()==""};

sets

b[] = {true, true, false};

why is b[2] false?

If A==B and A==C, should that not imply B==C?

Irrawaddy answered 17/10, 2019 at 16:55 Comment(2)
Related question : C++ if statements using strings not working as intendedAmiamiable
I see my mistake now and I've got a feeling of deja vu as it's not the first time I've confused myself comparing pointers.Irrawaddy
B
14

In this expression

s.c_str()==""

there are compared two pointers (addresses). The first one is the pointer returned by s.c_str() and the second one is the pointer to the first character (terminaring zero character) of the string literal "".

It is evident that the addresses are different (bear also in mind that the string literal has the static storage duration).

To get the expected result you should write instead

std::strcmp( s.c_str(), "" ) == 0

As for these two expressions

s==""

and

s==s.c_str()

then there are compared strings because the standard class std::string has overloaded operator == for the right operand.

Batik answered 17/10, 2019 at 16:58 Comment(8)
Does the standard guarantees the pointers to be different, in the last case ? I understand they can be.Giorgia
Can only add that it should be UB. "comparison with string literal results in unspecified behavior"Dehaven
@Giorgia It guarantteess because at least the string is empty.:) But in any case the class std::string uses a copy of an argument of its constructor.Batik
@Dehaven -- "UB" means undefined behavior". It means that the language definition does not tell you what the behavior of the **program is. A program with undefined behavior is not a valid C++ program. "Unspecified behavior" means that there are several alternatives, and the standard does not tell you which of those will be chosen. The program is valid, and the implementation can choose any of the alternatives.Sideling
@PeteBecker a program with UB is still valid C++ program (at least in the sense that it successfully compiles and can be run).Muncey
@Muncey -- the language definition says that it is not valid. Conforming compilers are allowed to compile invalid programs. Many compilers provide implementation-specific meanings to invalid code, generally with a warning, which is all that the standard requires, so you get an executable that you can run. That does not make the code valid.Sideling
@PeteBecker What do you mean by "language definition"? The C++ standard says that UB is "behavior for which this document imposes no requirements" (section 3.27), and well-formed program is "C++program constructed according to the syntax rules, diagnosable semantic rules, and the one-definition rule (6.2)" (3.29). So, UB does not breach any rules (which are contained in the standard) by definition, so a program containing UB is still well-formed (i.e. valid).Muncey
@Muncey -- if a program that has undefined behavior is "valid", why are conforming implementations allowed to refuse to compile them? Yes, you can play word games to make it look like "valid" means might compile and might not, depending, and if it compiles it might do something useful and might not, but that's a pretty useless definition.Sideling

© 2022 - 2025 — McMap. All rights reserved.