Can / should / will use ellipses for tuples?

In C ++ 11, variable templates allow you to call a function with any number of arguments, and an ellipsis operator ...allows this variational function to do something with each of these arguments, even if it is something not the same for each argument:

template<typename... Types>
void dummy(Types... ts){} //dummy to allow function calls via parameter expansion

template<typename... Numerics>
void increment5(Numerics&... ns){
    dummy(ns+=5 ...); //parameter expansion (need space after 5 because 5. is decimal)
    //it means "dummy(ns_first+=5, ns_second+=5, ns_third+=5, etc.)"
}

int main(){
    int i = 0;
    float f = 1.1;
    std::valarray<int> vi = {1,2,3};

    increment5(i,f,vi);

    cout
        <<i<<endl
        <<f<<endl
        <<vi[0]<<' '<<vi[1]<<' '<<vi[2]<<endl;

}

If we define a heterogeneous array of values ​​(a list of different types of numbers), we want to be able to do the same (to be able to add a number to each of its elements). But we must store the elements as a tuple.

//a class of numbers, possibly different
template<typename... Numerics>
class HeterogeneousValueArray{
private:
    tuple<Numerics...> inner;
public:
    //initialize the internal vector
    HeterogeneousValueArray(Numerics... ns): inner(ns...) {}
};

//Given this function, we don't have to explicitly give the types
//when constructing a HVA
template<typename... Numerics>
HeterogeneousValueArray<Numerics...> HeterogeneousValueArray(Numerics... ns){
    return HeterogeneousValueArray<Numerics...>(ns);
}

increment5 , . , , . 5, .

, , , . .

template<typename... Numerics>
void increment5(HeterogeneousValueArray<Numerics...>& hva){
    increment5(hva.inner... ); //expand a tuple
}

.

template<typename... Numerics>
void increment5(HeterogeneousValueArray<Numerics...>& hva){
    dummy((hva.inner+5)... ); //expand a tuple
}

, .

, " , ", , . , (?), ? ... ++ 14?

+4
1

++ 14, , , :

template<typename Tuple, std::size_t... I>
void
_increment5(Tuple& t, std::index_sequence<I...>)
{
  dummy( std::get<I>(t) += 5 ... );
}

template<typename... Numerics>
void
increment5(HeterogeneousValueArray<Numerics...>& hva)
{
  _increment5(hva.inner, std::index_sequence_for<Numerics...>());
}

std::index_sequence_for<T1, T2, T3> std::index_sequence<0, 1, 2>, . , std::get<I>(t)... I, .

index_sequence ++ 14 , apply , , @DyP :

template<typename... Numerics>
void
increment5(HeterogeneousValueArray<Numerics...>& hva)
{
  apply(hva.inner, [](auto& n) { n += 5; });
}

apply ++ 14 ( ++ 17), , ++ 14.

, , , N3728, , C + +14.

+5

All Articles