Andrew Tomazos
Andrew Tomazos

Reputation: 68638

Assignment of function template instantiation to function pointer?

Please consider the following C++20 program:

void (*p)(int x);

void f(auto x) {
    p = f;
}

int main() {
    f(4.2);
}

Is this ill-formed? What I would have expected to happen is that f(4.2) instantiates f as void(double), and so the assignment of p to f should be a type mismatch. But, g++ accepts this without warning.

What am I missing?

Update

For reference, the following program:

void (*p)(int x);

void g(double x);

int main() {
    p = g;
}

fails with error: invalid conversion from void (*)(double) to void (*)(int)

Upvotes: 4

Views: 69

Answers (1)

songyuanyao
songyuanyao

Reputation: 172924

so the assignment of p to f should be a type mismatch

No, p = f; causes the instantiation of void(int), then it's assigned to p.

Upvotes: 1

Related Questions