Date: Wed, 10 Jul 2024 21:50:51 +0300
On 7/10/24 20:38, organicoman wrote:
>
>>> template<void(*)(int)>
>>> struct A { // impl };
>
>>You can't. A function pointer points to a >function. You can't have a
>>pointer to a function template. So, at the >point when A is instantiated,
>>the function pointer must be either null or >point to a function, whether
>>it was generated as a result of a function >template instantiation or was
>>never a template to begin with.
>
> Yet a function pointer is compile time constant, that means we can
> examine its properties at compile time.
Yes, in the context of non-type template parameters.
> And the template arguments are
> among these properties.
No.
Ask yourself, what is the type of that value? The answer is obvious, it
is `void(*)(int)`, as written in the A's template parameter list.
Nowhere in that type, the original function template arguments are
mentioned. In fact, as I said before, that type does not even indicate
that the function was a template to begin with.
> This is related to type erasure.
Type erasure is a *runtime* programming technique, where the type of an
object is hidden from some part of the program. "Runtime" in this
context means that the transition of this abstraction layer can only
happen in run time.
There is no such thing as *compile time* type erasure.
> I think the compiler should allow a way to access the template arguments
> of a function pointer.
Again, this is not possible as this information is simply not present in
the type.
>
>>> template<void(*)(int)>
>>> struct A { // impl };
>
>>You can't. A function pointer points to a >function. You can't have a
>>pointer to a function template. So, at the >point when A is instantiated,
>>the function pointer must be either null or >point to a function, whether
>>it was generated as a result of a function >template instantiation or was
>>never a template to begin with.
>
> Yet a function pointer is compile time constant, that means we can
> examine its properties at compile time.
Yes, in the context of non-type template parameters.
> And the template arguments are
> among these properties.
No.
Ask yourself, what is the type of that value? The answer is obvious, it
is `void(*)(int)`, as written in the A's template parameter list.
Nowhere in that type, the original function template arguments are
mentioned. In fact, as I said before, that type does not even indicate
that the function was a template to begin with.
> This is related to type erasure.
Type erasure is a *runtime* programming technique, where the type of an
object is hidden from some part of the program. "Runtime" in this
context means that the transition of this abstraction layer can only
happen in run time.
There is no such thing as *compile time* type erasure.
> I think the compiler should allow a way to access the template arguments
> of a function pointer.
Again, this is not possible as this information is simply not present in
the type.
Received on 2024-07-10 18:51:05