Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
Дело в том, что хотя стандарт и гарантирует, что process() будет вызвана перед print_result(), но не гарантируется, что перед аргумент функции print_result будет вычислен после выполнения process().
When calling a function (whether or not the function is inline), there is a sequence point after the evaluation
of all function arguments (if any) which takes place before execution of any expressions or statements in
the function body. There is also a sequence point after the copying of a returned value and before the execution
of any expressions outside the function11)
[intro.execution] Par 15:
When calling a function (whether or not the function is inline), every value computation and side effect
associated with any argument expression, or with the postfix expression designating the called function, is
sequenced before execution of every expression or statement in the body of the called function. [ Note: Value
computations and side effects associated with different argument expressions are unsequenced. — end note ]
Every evaluation in the calling function (including other function calls) that is not otherwise specifically
sequenced before or after the execution of the body of the called function is indeterminately sequenced with
respect to the execution of the called function.
every value computation and side effect
associated with any argument expression, or with the postfix expression designating the called function, is
sequenced before execution of every expression or statement in the body of the called function
data == 185. И упорядочивание тоже это гарантирует (вызовы функций внутри одного полного выражения не пересекаются, выражение слева после точки вычисляется перед выражением справа).data + 2 может вычисляться как при data == 0 (сначала вычисляются все аргументы, потом вызывается process, потом print_resutl), так и при data == 185 (сначала аргументы process, потом вызов process, потом аргументы print_result, потом print_result).А при инициализации через конструктор, даже самой изощрённой, описанное в топике мало пригодится.
worker const & w = worker()
.set_data(data_t{})
.process()
.send_result()
.print_log();
Method chaining