简体   繁体   中英

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

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

Not exactly the same, but maybe you can push the sequence with an helper function as it 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>());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM