Hacker News new | ask | show | jobs
by muststopmyths 652 days ago
The result of the expression is the condition.

Thus, in your example, the bool check would apply to "s", after the expression is evaluated.

The fact that foo() may return nullptr at runtime and your "s" is UB is your fault for running with scissors.

so "this beast would work" for some definition of "work". But not because of order of evaluation.

Most modern C++ compilers would warn you about not using a bool in a conditional anyway.

1 comments

> The result of the expression is the condition. Thus, in your example, the bool check would apply to "s", after the expression is evaluated.

This is a contradiction. There is no expression in my code that evaluates to s. foo() is an expression, and then std::string s = ... is assignment initialisation, which is not an expression.

Edit: I suppose that if I used another form of initialisation, the answer becomes a bit more obvious:

   if (std::string s('x', 3))
(Not that this makes sense but just the point is to use a constructor with more than one argument.) In this case it's clear the test has to be the just-initialised variable. In fact there could be no arguments at all!
You are using definitions that I am not familiar with. Maybe it's because we speak different programming languages :-)

x = y is an expression statement in C++, which can be evaluated in an "if" for its side-effects.

https://en.wikipedia.org/wiki/Expression_(computer_science)

But Type x = y isn’t.