Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

c++ - 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)

question from:https://stackoverflow.com/questions/65950064/assignment-of-function-template-instantiation-to-function-pointer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...