I can not understand a piece of C ++ code with perfect forwarding and ellipses

I do not understand what the following C ++ code fragment does:

template<typename... Ts> void print(Ts &&... ts) { ns::logger{ (print(std::forward<Ts>(ts)), ns::s{})... }; } 

I see that there is perfect forwarding with variable arguments, but what happens in the line below for sure?

I assume that an object like ns::logger is a uniform, initialized series of values, but I'm not sure which one is ... folding expression?

+5
source share
1 answer

ns::logger initialized with a list of expressions (print(std::forward<Ts>(ts)), ns::s{}) , one for each element in ts .

Each expression, in turn, uses a comma operator . It calls print(std::forward<Ts>(ts)) and discards its result (if any). Then it creates ns::s{} , and this object becomes the result of the comma operator.

The end result is roughly equivalent to this pseudo-code:

 print(std::forward<Ts_1>(ts_1)); print(std::forward<Ts_2>(ts_2)); ... print(std::forward<Ts_N>(ts_N)); ns::logger{ns::s{}, ns::s{}, ... /* repeated N times */}; 
+4
source

Source: https://habr.com/ru/post/1271279/


All Articles