C++ Logo

std-proposals

Advanced search

Re: [std-proposals] template params of a function template

From: organicoman <organicoman_at_[hidden]>
Date: Wed, 10 Jul 2024 22:47:36 +0400
From my understanding of non type template parameters, and especially of function pointers, is that the compiler postpones the substitution of the actual value of the function pointer until the end.But during the compilation process it assumes that the pointer value is well known and resolved at link time.But what is interesting, is that during all that compile time processing, the function signature contains the template arguments.Actually when you don't specify the arguments of the function template, the compiler complains about overload ambiguity which means, A<foo<T1,T2>> a; A<foo<V1,V2>> b;Are totally different types. Even it they will have the same pointer value.By the way the same applies to templated variables.For me i don't see a difference between: A<foo<T1,T2>> a;And A<std::vector<T>> v;I can specialize 'A' to extract the type inside std::vector. Why couldn't I do the same for templated functions.This is useful in the following situation:template<typename T, typename V>void foo(int){}int main(){ constexpr auto f = &foo<double, char>; // assuming A is type trait struct using type_T = typename A<f>::T_ty; using type_V = typename A<f>::V_ty;}If we can adjust the compiler to allow this. It will make a lot of thing very easy to implement. Sent from my Galaxy
-------- Original message --------From: Tiago Freire <tmiguelf_at_[hidden]> Date: 7/10/24 9:54 PM (GMT+04:00) To: std-proposals_at_[hidden] Cc: organicoman <organicoman_at_[hidden]> Subject: RE: [std-proposals] template params of a function template

I’m going to be honest, haven’t looked at this in that close of a detail.
But here is something that standout quite obviously.
Addresses of functions are not constant expressions, and template arguments are required to be a constant expression known at compile time.
There’s no guarantee that the addresses of different functions are distinct, or have a certain order, that they exist at all, and in certain case they might not even be known until runtime.
So, I have absolutely no idea how you would envision this to work.
 
 
 


From: Std-Proposals <std-proposals-bounces_at_[hidden]>
On Behalf Of organicoman via Std-Proposals
Sent: Wednesday, July 10, 2024 19:39
To: Andrey Semashev via Std-Proposals <std-proposals_at_[hidden]>
Cc: organicoman <organicoman_at_[hidden]>
Subject: Re: [std-proposals] template params of a function template


 

 



>> 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. And the template arguments are among these properties.


This is related to type erasure.


I think the compiler should allow a way to access the template arguments of a function pointer.


Because when we pass a function pointer generated from a function template usually we do


 


A<foo<T1,T2>> a;


 


Which is mandatory to avoid overload ambiguity.


 


And at this point, we see clearly the template arguments, and i don't see why the compiler can't see them!


 



 


 



Sent from my Galaxy



 


 




Received on 2024-07-10 18:47:49