How do I check if a template parameter is a power of two?

后端 未结 3 945
刺人心
刺人心 2021-02-05 10:47

I want to create a structure that allocates statically an array of 2^N bytes, but I don\'t want the users of this structure to specify this size as the exponent

3条回答
  •  猫巷女王i
    2021-02-05 11:45

    You can use static_assert to provide an error message:

    template
    struct is_power_of_two {
        static_assert((N > 1) & !(N & (N - 1)), "Template parameter must be a power of two.");
    };
    

提交回复
热议问题