其他分享
首页 > 其他分享> > c – 我可以获取标准库中定义的函数的地址吗?

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>中.引入显式强制转换以选择所需的重载.)

解决方法:

简短的回答

没有.

说明

[namespace.std]说:

Let F denote a standard library function (07001), 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 (07002), addressof (07003), or a function-to-pointer standard conversion (07004).
— 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 (07005) 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 (07007).

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>.

没有任何地方明确指定一个可寻址功能.

因此,此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 ]

[expr.unary.op]/6

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库中形成指向成员函数的指针,则程序的行为是未指定的(可能是格式错误的).

(感谢comment指出这一点!)

标签:c20,c,language-lawyer,c-standard-library,unspecified-behavior
来源: https://codeday.me/bug/20190930/1836780.html