In C++, is the return type considered part of the function signature? and no overloading is allowed with just return type modified.
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Keeping track of variable instances
- Why does const allow implicit conversion of refere
- thread_local variables initialization
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
They are enough a part of the type that you can overload functions based on function pointer types that differ only by return type:
It depends if the function is a function template or not.
In C++ Templates -- the complete guides, Jusuttis provides a different definition of that given in the C++ standard, but with equivalent consequences:
We define the signature of a function as the the following information:
const
,volatile
, orconst volatile
qualification of the functionAs litb suggested, it's worth to clarify why the return type is part of the signature of a template function.
. That said, if the return type is a template parameter:
it's possibile to instantiate two function which differ only in the return type:
Not only: as rightly reported by litb, it is also possible to overload two template functions, which differ only in the return type, even if the return type is not a dependent name. Here's his example:
Normal functions do not include the return type in their signature.
(note: i've rewritten this answer, and the comments below don't apply to this revision - see the edit-history for details).
Introduction
However, the matter about functions and function declarations in the Standard is complicated. There are two layers that have to be considered:
The so-called function declaration may declare a function entity or a template entity. If a function entity is declared, then you either have to do with an explicit specialization of a function template (with all arguments specified), or a declaration of an ordinary function. If a template entity is declared, then you are declaring a primary function template, or an explicit specialization where some arguments are not specified. (This is very similar to the relation of "object declaration" and objects or references: The former may declare either an object or a reference. So an object declaration may not necessarily declare an object!).
The Standard defines the signature of a function to include the following at
1.3.10
:It's missing the return type in this definition, which is part of the signature of a function template specialization (i.e a function declaration that declares a function which is a specialization of a template), as pointed out by
14.5.5.1
(recent C++0x working papers fixed that already to mention the return type in1.3.10
too):So what exactly does a signature contain, again?
So, when we ask about the signature of a function, we have to give two answers:
Notice, however, that the return type, in any case, is a significant part of the type of a function. That is, the following is not valid:
When is an overload invalid if only the return type differs?
Major compilers currently reject the following code:
But accept the following code:
However, the Standard does forbid a function declaration that only differs in the return type (when defining when an overload is valid, and when not). It does not define precisely what "differs only by return type" means, though.
Standard paragraph references:
13.1
7/2
and7/5
14.5.5.1
For reference, here is what the most recent C++0x draft n3000 says about "signature" in
1.3.11
, which is much more complete in its coverage of the different type of entities: