问题描述
将函数指针或成员函数指针带到标准函数的现有规则是什么?例如,像
What are the existing rules for taking function pointers or member function pointers to Standard functions? For example, something like
auto p = &std::string::size;
这是合法的吗?如果我明确地请求正确的类型,它会或多或少是合法的,所以即使有额外的实现添加的重载 std :: string :: size
?
Is this legal? Would it be more or less legal if I explicitly requested the correct type, so it would function even if there was an additional implementation-added overload of std::string::size
?
推荐答案
使用正确类型不会使事情更好:除了 / code>函数标准C ++库中的所有函数都可以有额外的参数,只要这些参数是默认的。由于函数也可以使用额外的重载(除了
virtual
函数)来声明,因此可以尝试为重载设置一个变量。因此,代码不可移植,并且没有办法通过使用某种类型的转换或某些签名代替 auto
来移动它。
Using the "correct" type doesn't make things better: Except for the virtual
functions all functions in the standard C++ library can have additional arguments as long as these are defaulted. Since the functions can also be declared with additional overloads (again with the exception of the virtual
function), you can end up trying to assign an overload set a variable. Thus, the code isn't portable and there is no way to make it portable by using some sort of cast or some signature instead of auto
.
相关报价是17.6.5.5 [member.functions]第1段:
The relevant quote is 17.6.5.5 [member.functions] paragraph 1:
一个类似的非成员函数的权限,虽然。不知道在哪里许可混乱这些是隐藏,但我相对肯定,有一些狡猾的话,为这些。进一步看,似乎非成员函数根据17.6.5.4 [global.functions]段3更受约束:
I don't see a similar permission for non-member functions, though. Not sure where the permission to mess with these is hiding but I'm relatively sure that there are some weasel words for these as well. Looking further, it seems non-member functions are more constrained according to 17.6.5.4 [global.functions] paragraph 3:
这意味着您 >至少在指定所需签名时采用非成员函数的地址。
This would imply that you can take the address of the non-member functions, at least, when specifying the desired signature.
这篇关于函数指针和成员函数指针对标准函数的规则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!