我可以获取标准库中定义的函数的地址吗?
Can I take the address of a function defined in standard library?
考虑以下代码:
#include <cctype>
#include <functional>
#include <iostream>
int main()
{
std::invoke(std::boolalpha, std::cout); // #1
using ctype_func = int(*)(int);
char c = std::invoke(static_cast<ctype_func>(std::tolower), 'A'); // #2
std::cout << c << "\n";
}
此处,对 std::invoke
的两次调用都做了标记,以供将来参考。
预期输出为:
a
C++20 能保证预期的输出吗?
(注意:有两个函数称为 tolower
— 一个在 <cctype>
中,另一个在 <locale>
中。显式转换被引入到 select 所需的重载.)
简答
没有
说明
Let F
denote a standard library function ([global.functions]), a standard library static member function, or an instantiation of a standard library function template.
Unless F
is designated an addressable function, the behavior of a C++ program is unspecified (possibly ill-formed) if it explicitly or implicitly attempts to form a pointer to F
.
[Note: Possible means of forming such pointers include application of the unary &
operator ([expr.unary.op]), addressof
([specialized.addressof]), or a function-to-pointer standard conversion ([conv.func]).
— end note ]
Moreover, the behavior of a C++ program is unspecified (possibly ill-formed) if it attempts to form a reference to F
or if it attempts to form a pointer-to-member designating either a standard library non-static member function ([member.functions]) or an instantiation of a standard library member function template.
考虑到这一点,让我们检查对 std::invoke
.
的两次调用
第一次通话
std::invoke(std::boolalpha, std::cout);
在这里,我们试图形成一个指向 std::boolalpha
的指针。幸运的是,[fmtflags.manip] 挽救了局面:
Each function specified in this subclause is a designated addressable function ([namespace.std]).
而boolalpha
是本子条款中指定的函数。
因此,这一行是合式的,相当于:
std::cout.setf(std::ios_base::boolalpha);
但这是为什么呢?嗯,下面的代码是必须的:
std::cout << std::boolalpha;
第二次调用
std::cout << std::invoke(static_cast<ctype_func>(std::tolower), 'A') << "\n";
不幸的是,[cctype.syn] 说:
The contents and meaning of the header <cctype>
are the same as the C standard library header <ctype.h>
.
没有任何地方 tolower
明确指定了可寻址函数。
因此,此 C++ 程序的行为是未指定的(可能格式错误),因为它试图形成指向 tolower
的指针,该指针未指定为可寻址函数。
结论
无法保证预期的输出。
事实上,代码甚至不能保证编译。
这也适用于成员函数。
[namespace.std] 没有明确提及这一点,但从 [member.functions] 可以看出,如果 C++ 程序试图获取在 C++ 标准库中声明的成员函数。每 [member.functions]/2:
For a non-virtual member function described in the C++ standard library, an implementation may declare a different set of member function signatures, provided that any call to the member function that would select an overload from the set of declarations described in this document behaves as if that overload were selected. [ Note: For instance, an implementation may add parameters with default values, or replace a member function with default arguments with two or more member functions with equivalent behavior, or add additional signatures for a member function name. — end note ]
The address of an overloaded function can be taken only in a context that uniquely determines which version of the overloaded function is referred to (see [over.over]). [ Note: Since the context might determine whether the operand is a static or non-static member function, the context can also affect whether the expression has type “pointer to function” or “pointer to member function”. — end note ]
因此,如果程序显式或隐式地尝试形成指向 C++ 库中的成员函数的指针,则该程序的行为是未指定的(可能格式错误)。
(感谢 指出这一点!)
考虑以下代码:
#include <cctype>
#include <functional>
#include <iostream>
int main()
{
std::invoke(std::boolalpha, std::cout); // #1
using ctype_func = int(*)(int);
char c = std::invoke(static_cast<ctype_func>(std::tolower), 'A'); // #2
std::cout << c << "\n";
}
此处,对 std::invoke
的两次调用都做了标记,以供将来参考。
预期输出为:
a
C++20 能保证预期的输出吗?
(注意:有两个函数称为 tolower
— 一个在 <cctype>
中,另一个在 <locale>
中。显式转换被引入到 select 所需的重载.)
简答
没有
说明
Let
F
denote a standard library function ([global.functions]), a standard library static member function, or an instantiation of a standard library function template. UnlessF
is designated an addressable function, the behavior of a C++ program is unspecified (possibly ill-formed) if it explicitly or implicitly attempts to form a pointer toF
. [Note: Possible means of forming such pointers include application of the unary&
operator ([expr.unary.op]),addressof
([specialized.addressof]), or a function-to-pointer standard conversion ([conv.func]). — end note ] Moreover, the behavior of a C++ program is unspecified (possibly ill-formed) if it attempts to form a reference toF
or if it attempts to form a pointer-to-member designating either a standard library non-static member function ([member.functions]) or an instantiation of a standard library member function template.
考虑到这一点,让我们检查对 std::invoke
.
第一次通话
std::invoke(std::boolalpha, std::cout);
在这里,我们试图形成一个指向 std::boolalpha
的指针。幸运的是,[fmtflags.manip] 挽救了局面:
Each function specified in this subclause is a designated addressable function ([namespace.std]).
而boolalpha
是本子条款中指定的函数。
因此,这一行是合式的,相当于:
std::cout.setf(std::ios_base::boolalpha);
但这是为什么呢?嗯,下面的代码是必须的:
std::cout << std::boolalpha;
第二次调用
std::cout << std::invoke(static_cast<ctype_func>(std::tolower), 'A') << "\n";
不幸的是,[cctype.syn] 说:
The contents and meaning of the header
<cctype>
are the same as the C standard library header<ctype.h>
.
没有任何地方 tolower
明确指定了可寻址函数。
因此,此 C++ 程序的行为是未指定的(可能格式错误),因为它试图形成指向 tolower
的指针,该指针未指定为可寻址函数。
结论
无法保证预期的输出。 事实上,代码甚至不能保证编译。
这也适用于成员函数。 [namespace.std] 没有明确提及这一点,但从 [member.functions] 可以看出,如果 C++ 程序试图获取在 C++ 标准库中声明的成员函数。每 [member.functions]/2:
For a non-virtual member function described in the C++ standard library, an implementation may declare a different set of member function signatures, provided that any call to the member function that would select an overload from the set of declarations described in this document behaves as if that overload were selected. [ Note: For instance, an implementation may add parameters with default values, or replace a member function with default arguments with two or more member functions with equivalent behavior, or add additional signatures for a member function name. — end note ]
The address of an overloaded function can be taken only in a context that uniquely determines which version of the overloaded function is referred to (see [over.over]). [ Note: Since the context might determine whether the operand is a static or non-static member function, the context can also affect whether the expression has type “pointer to function” or “pointer to member function”. — end note ]
因此,如果程序显式或隐式地尝试形成指向 C++ 库中的成员函数的指针,则该程序的行为是未指定的(可能格式错误)。
(感谢