C ++ template type inference from function pointer arguments

I have a template that looks something like this:

template< typename T, void (*f)( T& param )>
class SomeAction
{
...
};

fused internally SomeAction(is actually fa member of the class, but I don’t think it matters).

Question: can this be improved by removing "typename T" from the list of template parameters and allowing the compiler to infer this type?

Thank!

+4
source share
1 answer

Perhaps the C ++ 17 function you're looking for is Declaring non-piggy type template parameters with auto

, , , SomeAction, T

template<auto> class SomeAction;
template<void (*f)(auto&)> class SomeAction<f> {};

void foo(int&) { /* bla */ }

int main()
{
    // C++17 only, no compiler support yet
    SomeAction<f> s; // T deduced to be int
}
+3

Source: https://habr.com/ru/post/1650737/


All Articles