Home >Backend Development >C++ >Can I Get the Address of a Standard Library Function in C ?

Can I Get the Address of a Standard Library Function in C ?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-25 00:13:11810browse

Can I Get the Address of a Standard Library Function in C  ?

Can I Take the Address of a Function Defined in Standard Library?

Overview

Can a C program take the address of a function defined in the standard library, and if so, under what conditions?

Short Answer

No, not in general.

Explanation

According to [namespace.std], forming a pointer to a standard library function is unspecified behavior unless the function is designated as addressable. This applies to member functions as well.

The First Call to std::invoke

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

In this example, std::boolalpha is an addressable function designated by [fmtflags.manip]. Therefore, this line is well-formed and equivalent to:

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

The Second Call to std::invoke

std::invoke(static_cast<ctype_func>(std::tolower), 'A');

However, for functions defined in , [cctype.syn] states that the specific functions are defined according to the C standard library header . Nowhere in is tolower designated as addressable, so the behavior of this program is unspecified (possibly ill-formed) due to the attempt to form a pointer to tolower.

Conclusion

The unexpected output is not guaranteed, and the code may not even compile. This also applies to member functions declared in the C standard library.

The above is the detailed content of Can I Get the Address of a Standard Library Function in C ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn