函数指针和成员函数指针对标准函数的规则是什么? [英] What are the rules for function pointers and member function pointers to Standard functions?

查看:144
本文介绍了函数指针和成员函数指针对标准函数的规则是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将函数指针或成员函数指针带到标准函数的现有规则是什么?例如,像

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:


实现可以声明额外非虚拟成员函数签名:

---通过向成员函数签名添加具有默认值的参数;

- 通过用默认值替换成员函数签名值由具有等效行为的两个或更多成员函数签名;和
- 通过为成员函数名添加成员函数签名。

An implementation may declare additional non-virtual member function signatures within a class:
--- by adding arguments with default values to a member function signature;
— by replacing a member function signature with default values by two or more member function signatures with equivalent behavior; and
— by adding a member function signature for a member function name.

一个类似的非成员函数的权限,虽然。不知道在哪里许可混乱这些是隐藏,但我相对肯定,有一些狡猾的话,为这些。进一步看,似乎非成员函数根据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:


不会使用其他默认参数声明全局或非成员函数签名。

An implementation shall not declare a global or non-member function signature with additional default arguments.

这意味着您 >至少在指定所需签名时采用非成员函数的地址。

This would imply that you can take the address of the non-member functions, at least, when specifying the desired signature.

这篇关于函数指针和成员函数指针对标准函数的规则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆