Using an int as a template parameter that is not known until run-time

后端 未结 5 1517
谎友^
谎友^ 2021-01-02 19:04

I am trying to use an integer as a template parameter for a class. Here is a sample of the code:

template< int array_qty > 
class sample_class {

    p         


        
5条回答
  •  攒了一身酷
    2021-01-02 19:36

    Template arguments must be compile-time constants aka "constant expressions" or constexprs for short. So there is no way to do is using templates.

    You could use a dynamic-sized array and store its size in an int.

    Or simply use a vector. Be sure to initialize its size in the constructor by passing the desired size to the vector's constructor!

提交回复
热议问题