Some const char * are unavailable at compile time?

前端 未结 2 1653
离开以前
离开以前 2021-01-03 23:16

Let\'s suppose we have a template function with non-type parameter of const char * like this:

template  void print()         


        
2条回答
  •  一个人的身影
    2021-01-03 23:44

    From the c++11 standard §14.3.2.1

    Template non-type arguments

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

    1. 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
    2. the name of a non-type template-parameter; or
    3. 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
    4. a constant expression that evaluates to a null pointer value (4.10); or
    5. a constant expression that evaluates to a null member pointer value (4.11); or
    6. a pointer to member expressed as described in 5.3.1; or
    7. an address constant expression of type std::nullptr_t.

    To your questions:

    Why the namespace_const_message and const_message aren't available at compile-time and thus forbidden in the print template function?

    That's why constexpr exists. They can be used where it's needed compile-time evaluation, thus available to be template-arguments.

    Is my guess about the string literals correct?

    There is a note about this right after the arguments:

    Note: A string literal (2.14.5) does not satisfy the requirements of any of these categories and thus is not an acceptable template-argument.

提交回复
热议问题