Search code examples
c++language-lawyertemplate-argument-deduction

How does template argument deduction for f(T&()) happen?


I was reading the standard, and couldn't figure out how the following code compiles.

template <class T>
void f(T&()) {}

double d = 0;
double& g() { return d; }

int main() {
    f(g);
}

[temp.deduct.type]/8 provides a list of cases where the deduction occurs:

A template type argument T, a template template argument TT or a template non-type argument i can be deduced if P and A have one of the following forms:
T
cv T
T*
T&
T&&
...

For P = T&() in my case, it doesn't seems to match any of the forms in the list.

Did I miss any rules for template deduction, or is the list not comprehensive?


Solution

  • This is covered in temp.deduct.type#11

    These forms can be used in the same way as T is for further composition of types.

    It might be clearer if you write your template with a different typename than T, e.g.

    template <class U>
    void f(U&()) {}
    

    so now you can see that U&() is of the form T(), where U& is a variant of T&.