Home > Backend Development > C++ > body text

Can You Use Function Pointers with Built-in Operators in C ?

Patricia Arquette
Release: 2024-10-25 10:43:02
Original
564 people have browsed it

Can You Use Function Pointers with Built-in Operators in C  ?

Can Built-in Standard Operators Have Function Pointers?

In C , you cannot directly refer to function pointers of built-in operators because they do not actually represent function objects. According to the C 11 standard, built-in operators are specifically designed for overload resolution and cannot be used for other purposes.

Overcoming the Limitation:

To work around this, you can use function objects defined in the standard library (§20.8). These function objects, such as equal_to for comparisons, decay to the equivalent operator in their operator() function. They can serve as valid arguments for function pointers.

Accessing Standard Class Operators:

While built-in operators are not directly accessible, you can utilize standard library operators as function pointers. However, you must specify the appropriate template instance to guide the compiler. For example, if you want to use the operator of std::basic_string, you would need to explicitly specify its template type as follows:

<code class="cpp">typedef std::basic_string<char> string_type;
...
std::cout << test_function<string_type>(a, b, &amp;std::operator+) << std::endl;</code>
Copy after login

Conclusion:

Although built-in operators cannot have direct function pointers due to their unique role, you can access their functionality through function objects provided by the standard library or by specifying the necessary template instances for standard class operators.

The above is the detailed content of Can You Use Function Pointers with Built-in Operators in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!