std :: bitset初始化的动态大小

时间:2019-05-30 17:33:54

标签: c++ dynamic types bitset

我无法分配带有计算得出的数字的模板大小。当我将值设置为固定数字时,它运行就没有问题

template<typename type>
    static type high_exponents(const type a, const type e, const type mod) {
        const type bits= log2(e); //<-----
        std::bitset<bits> bit_set(e);
    }
template<typename type>
    static type high_exponents(const type a, const type e, const type mod) {
        const type bits= 10000; // <-------
        std::bitset<bits> bit_set(e);
    }

这是我得到的错误

g++ -std=c++11 main.cpp -o output
In file included from rsa-crypt.h:3:0,
                 from main.cpp:2:
resources.h: In instantiation of 'static type resources::high_exponents(type, type, type) [with type = int]':
main.cpp:7:37:   required from here
resources.h:43:24: error: the value of 'n' is not usable in a constant expression
         std::bitset<n> bs;
                        ^~
resources.h:42:29: note: 'n' was not initialized with a constant expression
          const unsigned int n= (int) log2(e);
                             ^
resources.h:43:24: note: in template argument for type 'unsigned int'
         std::bitset<n> bs;
                        ^~
make: *** [output] Error 1

0 个答案:

没有答案