C++ Logo

std-proposals

Advanced search

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

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Wed, 10 Jul 2024 19:49:00 +0300
On 7/10/24 19:39, organicoman via Std-Proposals wrote:
> Hello Gents,
> Given this case:
> *****
> // primary template
> template<void(*)(int)>
> struct A;
>
> // specialization
> template<typename T, typename V
> ,template<typename, typename> F
> ,void (*(F<T,V>)(int)>
> struct A<F> { };
> ******
> Why this specialization doesn't compile?
> And how to extract the template params of a function template, let say
>
> template<typename T, typename V>
> void foo(int) {};
>
> In a templated struct that accepts function pointers, let say
>
> 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.

If you want to pass a statically polymorphic function as a class
template, you should use function objects:

  template< typename Func >
  struct A { /* Use Func here */ };

  struct F
  {
    template< typename T >
    void operator()(T) { }
  };

  A<F> a;

You can, of course, use a different vehicle for the function body, other
than operator(). For example, a static function template, if that fits
your use case better.

Received on 2024-07-10 16:49:03