Non-piggy type template parameter for polymorphic lambda?

Is it possible to write something like this?

[](std::index_sequence<std::size_t ...I> s) {

};

Or that?

[]<std::size_t ...I>(std::index_sequence<I...> s) { 

}

How is the syntax for this in C ++ 14 or C ++ 17? Or is it even impossible? Basically, I just want to have Ia template parameter package as a package, and lambda just serves that. Alternatively, is there a syntax to achieve the following?

std::index_sequence<std::size_t ...I> x = std::make_index_sequence<10>{};

// I now is a local template parameter pack
+4
source share
2 answers

GCC provides the latter syntax as an extension , but it is not standard:

template <typename... Ts>
void foo(const std::tuple<Ts...>& t) {
    auto l = [&t]<std::size_t ...I>(std::index_sequence<I...> s) { 
        std::initializer_list<int>{ (std::cout << std::get<I>(t), 0)... };
    };

    l(std::index_sequence_for<Ts...>{});
}

Live Demo

+2
source

Not exactly the same, but maybe you can click a sequence with an auxiliary function, as follows:

#include <functional>
#include <cstddef>
#include <iostream>

auto lambda = [](auto... I){
    int arr[] = { (std::cout << I << std::endl, 0)... };
    (void)arr;
};

template<std::size_t... I>
constexpr auto f(std::index_sequence<I...>) {
    return lambda(I...);
}

int main() {
    f(std::make_index_sequence<3>());
}
0

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


All Articles