且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

可以将包含函数指针的类用作非类型模板参数吗?

更新时间:2023-12-01 18:51:52

该代码有效.

[temp.arg.nontype]/2 :

非类型 template-parameter template-argument 应该是类型的转换后的常量表达式([expr.const]).模板参数.

[expr.const]/10 :

类型为 T 转换后的常量表达式是一个隐式转换为类型 T 的表达式,其中转换后的表达式为常量表达式,隐式转换序列仅包含[...]

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 [...]

(在这种情况下,没有隐式转换.)

(There's no implicit conversion in this case.)

[expr.const]/11 :

一个常量表达式是[...]或一个prvalue核心常量表达式,其值满足以下约束:

A constant expression is [...] or a prvalue core constant expression whose value satisfies the following constraints:

  • 如果该值是类类型的对象,则每个引用类型的非静态数据成员都引用一个实体,该实体是常量表达式的允许结果,
  • 如果该值是指针类型,则它包含[...]非立即函数的地址[...],
  • 如果该值是指向成员函数类型的指针,[...],
  • 如果值是类或数组类型的对象,则每个子对象都满足该值的这些约束.

如果[...]或它是非立即函数,则实体是常量表达式的允许结果.

An entity is a permitted result of a constant expression if [...] or if it is a non-immediate function.

(除了 consteval 功能之外,所有功能都不是直接的.

(All functions are non-immediate except for the consteval ones. [dcl.constexpr])

因此, s 是类型为 S 的有效转换常量表达式.此外,它不受 [temp.arg.nontype]/3的约束(仅适用于对象的指针/引用).

Therefore, s is a valid converted constant expression of type S. In addition, it is not subject to [temp.arg.nontype]/3 (which only applies to pointer/reference to objects).

也就是说, s 是有效的模板参数.

That is, s is a valid template argument.

我不确定错误消息指的是什么

I'm not sure what the error message is referring to

这是胡说八道.

invalid_tparm_referent_p .它是从实现类非类型模板参数时处理对象类型指针的代码中提取的( 4be5c72 ).显然,实现者忘记了更新此功能以解决指针指向功能的情况.

The error is emitted from invalid_tparm_referent_p inside GCC. It was extracted from code for handling pointer to object type when class non-type template paramter was implemented (4be5c72). Apparently, the implementer forgot to update this function to account for the pointer-to-function case.

我将错误报告为 https://gcc.gnu.org/PR97700 .