c++ - Iterating through parameters of a variadic function template using variadic lambda -
c++ - Iterating through parameters of a variadic function template using variadic lambda -
suppose have next function template:
template <typename functor, typename... arguments> void iteratethrough(functor functor, arguments&&... arguments) { // apply functor arguments }
this function implemented follows:
template <typename functor, typename... arguments> void iteratethrough1(functor functor, arguments&&... arguments) { int iterate[]{0, (functor(std::forward<arguments>(arguments)), void(), 0)...}; static_cast<void>(iterate); }
another way:
struct iterate { template <typename... arguments> iterate(arguments&&... arguments) { } }; template <typename functor, typename... arguments> void iteratethrough2(functor functor, arguments&&... arguments) { iterate{(functor(std::forward<arguments>(arguments)), void(), 0)...}; }
i have found yet approach uses variadic lambda:
template <typename functor, typename... arguments> void iteratethrough3(functor functor, arguments&&... arguments) { [](...){}((functor(std::forward<arguments>(arguments)), void(), 0)...); }
what pros , cons has method in comparing first two?
the calls functor
unsequenced. compiler can phone call functor
expanded arguments in order wants. example, iteratethrough3(functor, 1, 2)
functor(1); functor(2);
or functor(2); functor(1);
, whereas other 2 functor(1); functor(2);
.
section 8.5.4/4 of standard requires expressions within {}
initialiser evaluated left-to-right.
within initializer-list of braced-init-list, initializer-clauses, including result pack expansions (14.5.3), evaluated in order in appear.
section 5.2.2/4 states arguments function phone call can evaluated in order.
when function called, each parameter (8.3.5) shall initialized (8.5, 12.8, 12.1) corresponding argument. [note: such initializations indeterminately sequenced respect each other (1.9) — end note ]
this might not cover wording of order of evaluation (which can't find atm), known arguments functions evaluated in unspecified order. edit: see @dyp's comment relevant standard quote.
c++ templates c++11 lambda variadic-functions
Comments
Post a Comment