gpt4 book ai didi

c++ - 指向函数的指针的重载解析

转载 作者:行者123 更新时间:2023-11-30 03:57:25 24 4
gpt4 key购买 nike

当函数被调用时,很清楚名称查找和重载解析是如何执行的。但是当函数没有被调用时会发生什么?例如。

#include <iostream>

using std::cout;
using std::endl;

void foo(int){ cout << "foo(int)" << endl; }
void foo(int, int){ cout << "foo(int, int)" << endl; }

void (*baz)(int, int);


int main()
{
baz = foo; //1
baz(1, 1);
}

DEMO

在这种情况下,我们有两个名为 foo 的函数,正式地,非限定名称查找会找到它们。标准的第 13 条没有涵盖这种情况,因为它只涉及函数调用上下文 N3797:13.3/2 [over.match]:

Overload resolution selects the function to call in seven distinct contexts within the language:

最佳答案

这种情况下的行为受 C++11 标准中的措辞约束(N3797 中有类似的部分):

A use of an overloaded function name without arguments is resolved in certain contexts to a function, a pointer to function or a pointer to member function for a specific function from the overload set. ... The function selected is the one whose type is identical to the function type of the target type required in the context. -- ISO/IEC 14882:2011(E) §13.4 [over.over] (emphasis mine)

此处未使用标准重载解析规则,因为您要分配给函数指针类型,因此编译器将简单地选择与函数指针类型完全匹配的函数重载。

关于c++ - 指向函数的指针的重载解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28008073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com