<>斯特隆>;
答案取决于哪一种标准C++版本是你重新使用的。 值得注意的是,a
has staticstordur;c++20允许<><><>>>>>>>。
C++20
该方案是https://eel.is/c++draft/temp.arg#nontype-6”rel=“nofollow noreferer”>temp.arg.nontype :
For a non-type template-parameter of reference or pointer type, or for each non-static data member of reference
or pointer type in a non-type template-parameter of class type or subobject thereof, the reference or pointer
value shall not refer to or be the address of (respectively):
- a temporary object
- a string literal object
- the result of a
typeid
expression
- a predefined func variable or
- a subobject (6.7.2) of one of the above.
(斜体地雷)
Since &a[0]
is neither of the above things mentioned in the list, the program is well-formed.
还指出,模板论点必须是未标明的经常表述,此处确实如此。 https://timsong-cpp.github.io/cppwp/n4861/temp.arg#nontype-2”rel=“nofollow noreferer”>temp.arg:
A template-argument for a non-type template-parameter shall be a converted constant expression ([expr.const]) of the type of the template-parameter.
[ Note: If the template-argument is an overload set (or the address of such, including forming a pointer-to-member), the matching function is selected from the set ([over.over]).
— end note
]
注
A converted constant expression of type T is an expression, implicitly converted to type T, where the converted expression is a constant expression and the implicit conversion sequence contains only :
*
...
我们现在走到constant expression。
1. 经常表达是指通过不断表达(如下定义)而允许的某一实体,或指一种精细的核心不变表述,其价值取决于以下制约因素:
- if the value is of pointer type, it contains the address of an object with static storage duration, the address past the end of such an object ([expr.add]), the address of a non-immediate function, or a null pointer value,
C++17
在C++17,该方案按temp.arg:
A template-argument for a non-type template-parameter shall be a converted constant expression of the type of the template-parameter. For a non-type template-parameter of reference or pointer type, the value of the constant expression shall not refer to (or for a pointer type, shall not be the address of):
(斜体地雷)