小编fus*_*tee的帖子

为 constexpr std::array<size_t,3> 中的每个模板参数调用模板函数

给定一个函数print<size_t>(void)和 a ,我想要一个调用每个inconstexpr std::array<size_t,3> q={1,2,3}的循环。print<qi>qiq

我的最小示例如下所示:

#include<iostream>
#include<array>

template<size_t n>
void print(){ std::cout << n << "\n"; }

template< size_t N, const std::array<const size_t,N> q>
constexpr void print_long(){
    //
    // I) this is what I want to do
    for(size_t i=0;i<N;++i){ print<q[i]>(); }
    //
    // II) this does not compile either (because "it++")
    constexpr auto it = q.begin();
    while(it!=q.end()){ print<*(it++)>(); }
    //
    // III) for_each is no constexpr either
    std::for_each(q.begin(),q.end(),[](const auto& it){ print<it>(); }); …
Run Code Online (Sandbox Code Playgroud)

c++ for-loop compile-time function-templates constexpr

3
推荐指数
1
解决办法
116
查看次数