Regular spot(e) of C++ parameter battalion

Daily bit(e) of C++ | parameter pack

{getToc} $title={Table of Contents}
$ads={1}
Daily bit(e) of C++ #328, Representing any number of template arguments with a parameter pack.A parameter pack (C++11) is a template parameter that can represent any number of template arguments.Without fold...

Regular spot(e) of C++ #328, Representing immoderate quantity of template arguments with a parameter battalion.

A parameter battalion (C++Eleven) is a template parameter that tin correspond immoderate quantity of template arguments.

With out fold expressions (C++17), we person constricted choices for manipulating a parameter battalion.

#see <inferior>
#see <iostream>
#see <type_traits>

// Unimportant helpers
template <typename T> T change(T&& t) { instrument t; }
template <typename... Ts> void another(Ts... ts) {}
struct X {};

// Passing a parameter battalion about
template <typename... Ts>
void amusive(Ts&&... ts) {
// Walk to different relation.
another(ts...);
// Aforesaid however use a translation to all component.
another(change(std::guardant<Ts>(ts))...);
}

amusive(1, 2.Four, X{});
// another(1, 2.Four, X{});
// another(change(1), change(2.Four), change(X{}));

// Recursive processing
void recursive() {} // terminal government (thing to bash)

template <typename Actual, typename... Ts>
void recursive(Actual&& curr, Ts&&... ts) {
std::cout << curr << ", "; // procedure archetypal component
// and procedure the remainder recursively
recursive(std::guardant<Ts>(ts)...);
}

recursive(1, 2.Four, "Hullo Planet!");
// prints: 1, 2.Four, Hullo Planet!,

// Non-recursive processing:
template <typename T>
void procedure(T&& t) { std::cout << t << "\n"; }

template <typename... Ts>
void nonrecursive(Ts&&... ts) {
// Parameter packs tin beryllium expanded successful initializer lists
int dummy[sizeof...(Ts)] = {(procedure(std::guardant<Ts>(ts)), Zero)...};
// (procedure(std::guardant<Ts>(ts)), Zero) is an look that has
// a broadside-consequence of calling procedure(), however ever returns zero
}

nonrecursive(2.Four, 1);
// calls: procedure(2.Four), procedure(1) (successful that command [initializer_list])
// dummy == {Zero, Zero};

// Aggregate parameter packs tin beryllium expanded successful-measure
template <typename... Ts> struct battalion {};
template <typename A, typename B> struct brace {};

template <typename... TAs>
struct zip {
template <typename... TBs>
struct with {
typedef battalion<brace<TAs,TBs>...> kind;
};
};

typedef zip<int, treble>::with<int, int>::kind result_t;
// result_t == battalion<brace<int, int>, brace<treble, int>>

Unfastened the illustration successful Compiler Explorer.

$ads={2}
Previous Post Next Post

Formulario de contacto