c++ - In f(x), can x be evaluated before f? -


i have c++ program. program this:

struct myt {void memfunc(std::unique_ptr<myt> arg);}; std::unique_ptr<myt> obj = /* init */; obj->memfunc(std::move(obj)); 

is guaranteed valid, or can end calling member function on nullptr?
standard quotes applicable.
know order of evaluation of arguments unsequenced, don't recall sequencing w.r.t. function object being called.

pre-c++17, undefined behavior:

here quote proves evaluation neccessary call function , associated side-effects sequenced before function call.
also, other evaluations not sequenced indeterminately sequenced.
not impose ordering constraints on sub-expressions evaluated though respect each other, remain unsequenced respect each other.

1.9 program execution § 15

except noted, evaluations of operands of individual operators , of subexpressions of individual expressions unsequenced.
[...]
value computations of operands of operator sequenced before value computation of result of operator. if side effect on scalar object unsequenced relative either anotherside effect on same scalar object or value computation using value of same scalar object, behavior undefined.
when calling function (whether or not function inline), every value computation , side effect associated argument expression, or postfix expression designating called function, sequenced before execution of every expression or statement in body of called function.
[ note: value computations , side effects associated different argument expressions unsequenced. —end note ]
every evaluation in calling function (including other function calls) not otherwise sequenced before or after execution of body of called function indeterminately sequenced respect execution of called function.9 several contexts in c++ cause evaluation of function call, though no corresponding function call syntax appears in translation unit. [ ... ]
sequencing constraints on execution of called function (as described above) features of function calls evaluated, whatever syntax of expression calls function might be.

other relevant quotes std::move

template typename remove_reference::type&& move(t&& t) noexcept;
returns: static_cast<typename remove_reference::type&&>(t).

and std::unique_ptr<t>.operator->():

20.7.1.2.4 unique_ptr observers

pointer operator->() const noexcept;
requires: get() != nullptr.
returns: get().

memfunc gets argument value, have 3 calls:
a) obj->memfunc b) std::move(obj)
c) move constructor of passed argument.
because b) not change anything, can disregard argument:

a , c indeterminately sequenced, either can before other.
if happens first, good, c changing obj not matter.
if c happens first, evaluated zeroed obj, violating precondition, have ub.

in summary, undefined behavior, because 1 of allowed orders has undefined behavior.

post-c++17, well-defined:

8.2.2 function call [expr.call]

1 function call postfix expression followed parentheses containing possibly empty, comma-separated list of initializer-clauses constitute arguments function. [...]
[...]
5 postfix-expression sequenced before each expression in expression-list , default argument. [...]
[...]


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -