枚举的元编程问题

发布于 2024-12-02 04:44:17 字数 386 浏览 0 评论 0原文

Atm 我有这样的东西:

template<int n>
struct Pow
{
  enum{val= Pow<n-1>::val<<1};
};
template<>
struct Pow<0>{
    enum{val =1};
};

我可以访问像 Pow<30>::val 这样的数据。很好,但我想这样做

   int main()
    {
    Pow<30>::val;

,然后使用变量 访问所有值<0,30>; 我知道我可以使用数组和动态编程,但是我可以用这种方式做到这一点吗? 对不起英语。

Atm i have sth like that:

template<int n>
struct Pow
{
  enum{val= Pow<n-1>::val<<1};
};
template<>
struct Pow<0>{
    enum{val =1};
};

I can acess data like Pow<30>::val. It's good but i want do like this

   int main()
    {
    Pow<30>::val;

and then use variable to
access all of value <0,30>
I knew that i can use array and dynamic programming but can i do that in this way?
Sorry for English.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星軌x 2024-12-09 04:44:17

使用 C++0x 可变参数模板:

template<int... Indices>
struct powers {
    static const int value[sizeof...(Indices)];

    typedef powers<Indices..., sizeof...(Indices)> next;
};

template<int... Indices>
const int powers<Indices...>::value[sizeof...(Indices)] = { Pow<Indices>::val... };

template<int N>
struct build_powers {
    typedef typename build_powers<N - 1>::type::next type;
};

template<>
struct build_powers<1> {
    typedef powers<0> type;
};

然后:

int
main()
{
    // we want [0..30] inclusive so pass 31 as exclusive upper limit
    typedef build_powers<31>::type power_type;
    // 0..30 is 31 powers in all
    typedef const int array_type[31];

    array_type& ref = power_type::value;
    // ref[0] .. ref[30] are the values of Pow<0>::val .. Pow<30>::val
}

这就是使用数组但没有动态初始化。由于您希望结果作为变量而不是 TMP,我认为这已经足够了。

Using C++0x variadic templates:

template<int... Indices>
struct powers {
    static const int value[sizeof...(Indices)];

    typedef powers<Indices..., sizeof...(Indices)> next;
};

template<int... Indices>
const int powers<Indices...>::value[sizeof...(Indices)] = { Pow<Indices>::val... };

template<int N>
struct build_powers {
    typedef typename build_powers<N - 1>::type::next type;
};

template<>
struct build_powers<1> {
    typedef powers<0> type;
};

and then:

int
main()
{
    // we want [0..30] inclusive so pass 31 as exclusive upper limit
    typedef build_powers<31>::type power_type;
    // 0..30 is 31 powers in all
    typedef const int array_type[31];

    array_type& ref = power_type::value;
    // ref[0] .. ref[30] are the values of Pow<0>::val .. Pow<30>::val
}

So that's with using an array but without dynamic initialization. Since you want the result as a variable and not for TMP I feel this is adequate.

把昨日还给我 2024-12-09 04:44:17

当您执行 Pow<30>::val; 时,您将实例化两个模板的顶部,然后当它为零时,它将实例化专业化,并且只有最终结果在 < em>运行时,因为模板是在编译时解析的

When you do Pow<30>::val; you will instantiate the top of your two templates, then when it get's to zero it will instantiate the specialization, and only the final result will be visible at runtime, since templates are resolved at compile time

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文