463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122143

How to infer the template parameter from instantiation of function template?

I know how to infer the template parameter from an instantiation of a class template:

template <typename T>
struct foo {};

template <typename T>
struct foo_param;

template <typename T>
struct foo_param< foo<T> > {
    using type = T;
};

But I am lost at doing the same for a function template. The naive

template <typename T>
void bar() {}

template <auto F>
struct bar_param;

template <typename T>
struct bar_param< &bar<T> > {
    using type = T;
};

fails with

<source>:21:19: error: template argument '& bar<T>' involves template parameter(s)
   21 | struct bar_param< &bar<T> > {
      |                   ^~~~~~~

I think I do understand the error (actually it turned out that I didn't but thats a case for a different question), but I don't know how to avoid it. How can I infer eg int given a &bar<int> ?

Upvotes: 3

Views: 115

Answers (1)

max66
max66

Reputation: 66200

I don't think it's possible what do you want.

Not passing through the type of the function, at least, because the type of bar<T> is exactly the same for every type: a returning void function with no-argumens.

You can verify this with a simple static_assert()

static_assert( std::is_same_v<decltype(bar<int>), decltype(bar<long>)> );

Upvotes: 2

Related Questions