Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-type template parameter for polymorphic lambda?

Is it possible to write something like this?

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

};

Or this?

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

}

How is the syntax for this in C++14 or C++17? Or is it not possible at all? Basically, I just want to have the I as a template parameter pack, and the lambda just serves as a way to do 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
like image 719
Johannes Schaub - litb Avatar asked Jul 28 '16 08:07

Johannes Schaub - litb


1 Answers

GCC provides the latter syntax as an extension, but it's 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

like image 125
TartanLlama Avatar answered Sep 28 '22 11:09

TartanLlama