jeudi 27 février 2020

Iterate through std::initializer_list

//parameter pack sum example

constexpr int sum(int N= 0)
{
    return N;
}
template<typename ...Args>
constexpr int sum(int first, int second, Args ...N)
{
    return first + second + sum(N...);
}

int main()
{
    std::cout << sum<int>(1,6,3);
}

Is it possible to make this sum at compile time with std::initializer_list<int> how can i iterate recursive through this.

Aucun commentaire:

Enregistrer un commentaire