0

independent_bits_engine is the template class

template<
    class Engine, 
    std::size_t W, 
    class UIntType
> class independent_bits_engine;

The second parametr define, how much bits need to be generated. I don't know its. I would to ask this question to user in runtime. How do it?

  • Template parmeters are compile time, so I don't think you can set this at run time. If you want to generate a given number of bits, to be decided are runtime there are other tools to do this. – doctorlove Mar 29 '17 at 09:40

2 Answers2

0

You cannot. Template arguments have to be defined at compile-time.

To work around this, you can look at this post and answer: Specify template parameters at runtime

The idea is to use if statements to check the runtime value:

template<class Engine, class UIntType>
independent_bits_engine CreateEngine(std::size_t w) {
    if (w == 0) {
        return independent_bits_engine<Engine, 0, UIntType>();
    } else if (w == 1) {
        return independent_bits_engine<Engine, 1, UIntType>();
    }
    Etc;
}

If W can be a big value, you may want to use boost::preprocessor to generate the ifs (example given in the link).

Note that, the more the maximum value of W is big, the more independent_bits_engine classes will be instantiated, and the more the compilation will be slow, so you may want to switch to a full runtime solution instead.

Community
  • 1
  • 1
Romain Gros
  • 126
  • 1
  • 5
  • When you post a link as a solution or as a work-around, you should also explain it in a few words, it will be much more useful. – yakobom Mar 29 '17 at 10:18
0

The c++ templates are expanded at compile time, just like MICRO: c++ template explanation wikipedia

When you run the c++ program the templates params are already determined.

So if you insist on the using the template to implement the independent_bits_engine, you have to remove the template param std::size_t W. And instand you can pass the size param to the independent_bits_engine object at runtime.

Community
  • 1
  • 1
Bruce
  • 65
  • 1
  • 9