edit Possibly can\'t be done, see Clean implementation of function template taking function pointer although answer 1 there has a C macro work-around https://stacko
I think for c++11 it is impossible to achieve what you want.
templateclass unique_gptr : public std::unique_ptr { public: unique_gptr(T* t) : std::unique_ptr (t, D) { }; }; using int_gptr = unique_gptr ; int_gptr ig(new int(2));
Notice that decltype is applied on D, which is declared as a typename. So D is a type. But decltype can't be used on a type.
Firstly the code tries to get the address of a type (&). Secondly, the argument of decltype is expected to be an expression, but not a type or "the address of a type". To make it easier to understand, we can say that decltype expects its argument to be a “variable”, like the following example.
int main()
{
int i = 10;
decltype(i) j;
decltype(int) k; /* Compiler error. */
decltype(&int) l; /* Compiler error. */
return 0;
}
You also want the compiler to replace D with ::free_int. And ::free_int is passed in as a non-type template argument. However D is a type template parameter. A non-type template parameter starts with an actual type (e.g. int, struct a* or a type template name). While a type template parameter starts with typename or class. An easier example for non-type template parameter,
template
void func2(void)
{
decltype(INIT) j = INIT;
}
int main()
{
func2<10>();
return 0;
}
When you pass in a function pointer like ::free_int, you need a non-type template parameter, which must be preceded by a type. And you want the type of the function pointer to be "replaceable". So the type of the function pointer has to be a type template parameter. These make them two template parameters.
That's the reason you need three template parameters in template, which is the best result you have.