Home >Backend Development >C++ >Does C Function Overloading Allow Differences Only in Return Type?
Is the Return Type Included in the Function Signature and Does It Allow Overloading with Only Return Type Modifications?
In C , there's a distinction between function declarations and function entities. Declarations can establish function entities or template entities. For non-template function declarations, the signature encompasses the argument types, class membership information (if applicable), and qualifiers. However, for function template specializations, the signature also embraces the return type.
Function Signature for Function Specializations
Standard C dictates that for function template specializations, the signature involves the return type, as highlighted in section 14.5.5.1. This encompasses the signature of the template and the provided template arguments. This is in alignment with the understanding that the return type contributes to the type of a function.
Overloading with Differentiated Return Types
According to the Standard, function declarations with varying return types are considered distinct in terms of overloading (section 13.1). However, the definition of "differs only by return type" remains unspecified in the language.
Compiler Behavior
Currently, major compilers reject function declarations with identical signatures but differing return types (e.g., "int f();", "double f();"). However, this behavior may not necessarily be mandated by the Standard.
C 0x Draft Proposal
Recent C 0x draft specifications provide a more comprehensive definition of "signature" in section 1.3.11, explicitly including the return type for both function entities and template specializations. This coincides with the assertion that the return type forms an integral part of the function's type.
Therefore, in summary:
The above is the detailed content of Does C Function Overloading Allow Differences Only in Return Type?. For more information, please follow other related articles on the PHP Chinese website!