Home > Back-end >  Deduce template parameter for a template function pointer with default template parameter
Deduce template parameter for a template function pointer with default template parameter

Time:09-17

Though the question seems a little bit confusing. The code is simple as:

template <typename T>
void tfunc(T&& getter)
{

}

template <typename T = void>
void voidfunc()
{}

int main() {
    tfunc(&voidfunc);   // error: could not deduce template argument for 'T'
    tfunc(&voidfunc<int>); // ok
    voidfunc(); // calling using default template parameter is ok.
}

Both clang 11& msvc visual studio 2019 16.7 complains error. Why I need to explicitly specify a template argument.

BackGround

  • the argument is a dummy parameter just to delay the instantiation of voidfunc to where it is used. The type doesn't really matter.
  • &voidfunc is used by some code generated by clangAST, otherwise I need to tweak the generator to write &voidfuc<> if it is a template.
  • __declspec(property(put=voidfunc)) this clang/msvc extension however takes voidfunc but not voidfunc<>

I've tweaked the generator to output &voidfunc<> as @Jarod42 says, if the given function is a template. And it works for now.

CodePudding user response:

As stated in the comments, this situation was not clear in the standard until C 20, where it was cleaned up in order to better support the new feature of constrained functions. The new specification makes sense in previous language versions (ignoring the possibility of constraints), so hopefully implementations will eventually support this usage everywhere.

  • Related