C ++ 11:具有对数评估深度的编译时间数组

编程入门 行业动态 更新时间:2024-10-19 12:45:49
本文介绍了C ++ 11:具有对数评估深度的编译时间数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

一种实现C ++ 11数组的方法是使用模板,该数组的元素由编译器计算的其索引的函数初始化,并且结果存储在应用程序映像的数据段(.rodata)中,部分专门化和constexpr如下:

One way to implement a C++11 array that has its elements initialized by a function of their index calculated by the compiler and have the results stored in the data section (.rodata) of the application image is to use templates, partial specialization and constexpr as follows:

#include <iostream> #include <array> using namespace std; constexpr int N = 1000000; constexpr int f(int x) { return x*2; } typedef array<int, N> A; template<int... i> constexpr A fs() { return A{{ f(i)... }}; } template<int...> struct S; template<int... i> struct S<0,i...> { static constexpr A gs() { return fs<0,i...>(); } }; template<int i, int... j> struct S<i,j...> { static constexpr A gs() { return S<i-1,i,j...>::gs(); } }; constexpr auto X = S<N-1>::gs(); int main() { cout << X[3] << endl; }

这不适用于大的N值:

error: constexpr evaluation depth exceeds maximum of 512

这是因为递归模板评估的头尾形式,它具有N的线性深度。

This is because of the head-tail style of recursive template evaluation, that has linear depth in terms of N.

有办法做这样的评估深度是对数的N,而不是线性? (因此会避免默认的深度限制)

Is there a way to do it such that the evaluation depth is logarithmic in terms of N, rather than linear? (and hence would avoid the default depth limit)

推荐答案

如果你在代码中使用的是一个奇怪的形式下面是一个具有 O(log N)实例的实现:

If what you're using in the code is a weird form of the indices trick, here's an implementation that has O(log N) instantiations:

// using aliases for cleaner syntax template<class T> using Invoke = typename T::type; template<unsigned...> struct seq{ using type = seq; }; template<class S1, class S2> struct concat; template<unsigned... I1, unsigned... I2> struct concat<seq<I1...>, seq<I2...>> : seq<I1..., (sizeof...(I1)+I2)...>{}; template<class S1, class S2> using Concat = Invoke<concat<S1, S2>>; template<unsigned N> struct gen_seq; template<unsigned N> using GenSeq = Invoke<gen_seq<N>>; template<unsigned N> struct gen_seq : Concat<GenSeq<N/2>, GenSeq<N - N/2>>{}; template<> struct gen_seq<0> : seq<>{}; template<> struct gen_seq<1> : seq<0>{}; // example template<unsigned... Is> void f(seq<Is...>); int main(){ f(gen_seq<6>()); }

实例。

更多推荐

C ++ 11:具有对数评估深度的编译时间数组

本文发布于:2023-07-29 00:42:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235636.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对数   数组   深度   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!