Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I take the address of a function defined in standard library?

Consider the following code:

#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"; } 

Here, the two calls to std::invoke are labeled for future reference. The expected output is:

a 

Is the expected output guaranteed in C++20?

(Note: there are two functions called tolower — one in <cctype> and the other in <locale>. The explicit cast is introduced to select the desired overload.)

like image 595
L. F. Avatar asked Apr 15 '19 10:04

L. F.


People also ask

Which function is known as standard library function in C++?

string. The header string defines the class string of the C++ Standard Library. list, vector, stack, queue, deque, map, set, bitset. All these headers are used for Standard Template Library (STL) implementation. Each of these headers contains the respective class definition and function prototypes.

Is Main () a library function?

main is not a library function. However, the standard also imposes some requirements on its signature, and that it must not be overloaded or declared static or inline. In particular, it must not be used, meaning that you cannot call it.

How is it used in function library?

Answer: A function library is a separate object type in the ES Repository and enables mapping developers to use user-defined functions across message mappings. You can use user-defined functions from a function library in message mappings and in mapping templates.


1 Answers

Short answer

No.

Explanation

[namespace.std] says:

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.

With this in mind, let's check the two calls to std::invoke.

The first call

std::invoke(std::boolalpha, std::cout); 

Here, we are attempting to form a pointer to std::boolalpha. Fortunately, [fmtflags.manip] saves the day:

Each function specified in this subclause is a designated addressable function ([namespace.std]).

And boolalpha is a function specified in this subclause. Thus, this line is well-formed, and is equivalent to:

std::cout.setf(std::ios_base::boolalpha); 

But why is that? Well, it is necessary for the following code:

std::cout << std::boolalpha; 

The second call

std::cout << std::invoke(static_cast<ctype_func>(std::tolower), 'A') << "\n"; 

Unfortunately, [cctype.syn] says:

The contents and meaning of the header <cctype> are the same as the C standard library header <ctype.h>.

Nowhere is tolower explicitly designated an addressable function.

Therefore, the behavior of this C++ program is unspecified (possibly ill-formed), because it attempts to form a pointer to tolower, which is not designated an addressable function.

Conclusion

The expected output is not guaranteed. In fact, the code is not even guaranteed to compile.


This also applies to member functions. [namespace.std] doesn’t explicitly mention this, but it can be seen from [member.functions] that the behavior of a C++ program is unspecified (possibly ill-formed) if it attempts to take the address of a member function declared in the C++ standard library. Per [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 ]

And [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 ]

Therefore, the behavior of a program is unspecified (possibly ill-formed) if it explicitly or implicitly attempts to form a pointer to a member function in the C++ library.

(Thanks for the comment for pointing this out!)

like image 170
L. F. Avatar answered Sep 27 '22 22:09

L. F.