【发布时间】:2015-07-20 12:30:11
【问题描述】:
有没有办法制作一个无符号整数的constexpr-Array 来满足 constexpr 布尔函数 pred(std::size_t) 给出的某些谓词?
我尝试了很多,尤其是indices trick,只是为了发现我的数据太大,以至于超过了递归模板实例化限制256。我将无法更改此限制,如果可能的话,可以改变它。
正如 cmets 中所要求的,这是我想要实现的一些伪代码:
template<std::size_t... Is>
struct Sequence{};
template<std::size_t N, std::size_t... Is>
struct SequenceGenerator : SequenceGenerator<N-1, N-1, Is...>
{}; //obviously here it gets too deep into recursion, as mentioned
template<std::size_t... Is>
struct SequenceGenerator<0, Is...> : Sequence<Is...>
{};
template<std::size_t N>
struct MyData
{
std::size_t values[N];
static constexpr std::size_t size()
{ return N; }
};
template<typename Lambda, std::size_t... Is>
constexpr MyData<sizeof...(Is)> MyGen(Sequence<Is...>, Lambda func)
{
if(func(Is)...)
return {{ Is... }};
else
return /*some not-invalidating but current element discarding thing*/
}
template<std::size_t N, typename Lambda>
constexpr Generator<N> MyGen(Lambda func)
{
return MyGen(SequenceGenerator<N>(), func);
}
constexpr bool pred(std::size_t i) noexcept
{
//some condition making up a "range"
return i < 360ULL && i > 2ULL;
}
【问题讨论】:
-
你能告诉我们一些你想要实现的(伪)代码吗?也许使用不超过限制但其他工作的小型数据集并向我们展示您的意思?
-
@DanielFrey 添加了代码
-
那么您的想法是通过给定谓词“过滤”范围吗?此外,如果您正在寻找高效的“序列生成器”,我编写了一个库,提供one with logarithmic instantiation depth。
-
@Columbo 是的,但是范围大于 256,显然必须在编译时。
标签: c++ c++11 variadic-templates template-meta-programming compile-time