can a class containing a function pointer be used as a non-type template parameter?

The code is valid.

[temp.arg.nontype]/2:

A template-argument for a non-type template-parameter shall be a converted constant expression ([expr.const]) of the type of the template-parameter.

[expr.const]/10:

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:

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

  • if the value is an object of class type, each non-static data member of reference type refers to an entity that is a permitted result of a constant expression,
  • if the value is of pointer type, it contains […] the address of a non-immediate function […],
  • if the value is of pointer-to-member-function type, […],
  • if the value is an object of class or array type, each subobject satisfies these constraints for the value.

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

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

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).

That is, s is a valid template argument.


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

It’s nonsense.

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.

I’ve reported the bug as https://gcc.gnu.org/PR97700.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top