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

后端 未结 5 1534
谎友^
谎友^ 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:31

    For C++ 11, non-type template arguments are restricted to the following (§14.3.2/1):

    A template-argument for a non-type, non-template template-parameter shall be one of:

    • for a non-type template-parameter of integral or enumeration type, a converted constant expression (5.19) of the type of the template-parameter; or
    • the name of a non-type template-parameter; or
    • a constant expression (5.19) that designates the address of an object with static storage duration and external or internal linkage or a function with external or internal linkage, including function templates and function template-ids but excluding non-static class members, expressed (ignoring parentheses) as & id-expression, except that the & may be omitted if the name refers to a function or array and shall be omitted if the corresponding template-parameter is a reference; or
    • a constant expression that evaluates to a null pointer value (4.10); or
    • a constant expression that evaluates to a null member pointer value (4.11); or
    • a pointer to member expressed as described in 5.3.1.

    In C++ 98 and 03, the list is even more restricted. Bottom line: what you're trying to do simply isn't allowed.

提交回复
热议问题