Topic: Only function declarations may be overloaded;
Author: James Kanze <james.kanze@gmail.com>
Date: Mon, 23 Nov 2009 11:25:02 CST Raw View
On Nov 11, 6:49 am, Kaz Kylheku <kkylh...@gmail.com> wrote:
> On 2009-11-10, Johannes Schaub (litb) <schaub-johan...@web.de> wrote:
> > Hello all. I'm confused by 13/1 in the c++0x draft and in
> > c++03, which says:
> > "Only function declarations can be overloaded; object and
> > type declarations cannot be overloaded."
> > But we can also overload function templates.
> The unqualified term function includes both template and
> non-template functions.
That seems to be the case here, but in general, the standard
very definitely distinguishes between function declarations and
function template declarations (see for example 3.1.1/4).
I think it's a valid complaint (but probably only editorial).
--
James Kanze
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Wed, 25 Nov 2009 14:46:38 CST Raw View
James Kanze wrote:
> On Nov 11, 6:49 am, Kaz Kylheku <kkylh...@gmail.com> wrote:
>> On 2009-11-10, Johannes Schaub (litb) <schaub-johan...@web.de> wrote:
>
>> > Hello all. I'm confused by 13/1 in the c++0x draft and in
>> > c++03, which says:
>
>> > "Only function declarations can be overloaded; object and
>> > type declarations cannot be overloaded."
>
>> > But we can also overload function templates.
>
>> The unqualified term function includes both template and
>> non-template functions.
>
> That seems to be the case here, but in general, the standard
> very definitely distinguishes between function declarations and
> function template declarations (see for example 3.1.1/4).
>
It seems to me that in 3.3.1/4 that it does refer to the entity declared,
but not the the declaration itself (since it says "shall all refer" and not
"shall all be").
It looks like a similar case as with "object declaration": A declaration
declaring a reference of course does not declare an object, but it is still
called an object declaration by 7/7.
It surely is confusing, though.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Thu, 19 Nov 2009 13:56:59 CST Raw View
Johannes Schaub (litb) wrote:
> Hello all. I'm confused by 13/1 in the c++0x draft and in c++03, which
> says=
> :
>
> "Only function declarations can be overloaded; object and type
> declarations cannot be overloaded."
>
> But we can also overload function templates. Does the above apply to the
> contained function declarations found within function template
> declarations= ?
After screwing my head around this, i came to the following conclusion:
* A declaration which does contain a simple-declaration not nested in a
nested declarative region, and which gives the name declared a function type
(may be a dependent function type) is a function declaration (7/2 and 7/5).
Examples:
template<typename T> void f(); // function declaration
struct foo { void f(); }; // not a function declaration.
Another question is whether function templates are also functions on the
entity level. That is, whether function template declarations aren't only
function declarations, but whether such declarations also declare functions,
which is difficult for me to crasp. So does the following program actually
declare a function, or does it only declare a function template as a
function declaration?
template<typename T> void f();
On the other side, there are still texts that differentiate on the entity
level between functions and function templates; for instance, 1.3/11 in the
current c++0x draft, which could be worded like
"... If a function is a class member its signature additionally includes the
cv-quali ers (if any) and the ref-quali er (if any) on the function itself
..."
and also 3.4.2/2 which says "... In addition, if the argument is the name or
address of a set of overloaded functions and/or function templates,...".
It strikes me as being strange that those paragraphs all apparently contain
redundant text. What do i still miss about it?
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Tue, 10 Nov 2009 16:59:28 CST Raw View
Hello all. I'm confused by 13/1 in the c++0x draft and in c++03, which says=
:
"Only function declarations can be overloaded; object and type declarations
cannot be overloaded."
But we can also overload function templates. Does the above apply to the
contained function declarations found within function template declarations=
?
If that's the intended meaning, does 13.1/2b1 mean when it says "=97 Functi=
on
declarations that differ only in the return type cannot be overloaded." tha=
t
the following is ill-formed:
template<typename T> void f();
template<typename T, typename U> void f();
? I can't seem to spot a difference between the two "function declarations"=
,
if 13/1 is meant to apply to these, ignoring the surrounding template
clause. If not, then wouldn't it forbid overloading function templates
altogether, contradicting 14.5.5 which does allow it?
Isn't "function declaration" a semantical property of a declaration, which
does not apply to containd declarations found in function template
declarations, because such a declaration declares a template, but not a
function?
Can someone please help me figure it out? Thanks in advance.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Kaz Kylheku <kkylheku@gmail.com>
Date: Wed, 11 Nov 2009 00:49:53 CST Raw View
On 2009-11-10, Johannes Schaub (litb) <schaub-johannes@web.de> wrote:
> Hello all. I'm confused by 13/1 in the c++0x draft and in c++03, which says=
>:
>
> "Only function declarations can be overloaded; object and type declarations
> cannot be overloaded."
>
> But we can also overload function templates.
The unqualified term function includes both template and non-template
functions. In situations that apply to only one or the other, the text
makes it clear.
We can overload function templates, with other function templates as
well as with non-template functions.
This is given in 14.5.5 of c++03.
> Does the above apply to the
> contained function declarations found within function template declarations=
> ?
Of course.
> If that's the intended meaning, does 13.1/2b1 mean when it says "=97 Functi=
> on
> declarations that differ only in the return type cannot be overloaded." tha=
> t
> the following is ill-formed:
>
> template<typename T> void f();
> template<typename T, typename U> void f();
The above functions differ, in some aspect /other/ than the return type.
Namely, they differ in their template argument list.
A similar example appears in paragraph 4 of 14.5.5.1.
template<class T> void f();
template<int I> void f(); // OK: overloads the first template
// distinguishable with an explicit template
// argument list
The comment means that although template deduction cannot figure out
the arguments automatically, since the template arguments do not play any
role in the function argument list, explicit template arguments may be
given to choose one function or the other:
f<some_class>(); // call an instantiation of first overload above
f<3>(); // call an instantiation of second overload above
Your f's can be distinguished in the number of template arguments:
f<int>f(); // your first f
f<int, int>f(); // your second f
> ? I can't seem to spot a difference between the two "function declarations"=
The template parameter list difference /is/ a real difference.
The rule in question seems to prohibit situations like this:
template<typename T> void f();
template<typename T> int f();
These two differ ``only in the return type''. All else is the same: same
argument signature, same template parameters.
Implementation note: g++ 4.3.2 accepts the above declarations, without
a diagnostic. This appears to be a small noncoformance. However, you get a
diagnostic if you try to use f.
There is no unambiguous way to call a particular one of the two f's
above: For the call
f<int>()
Regardless of what --std option, g++-4.3.2 -Wall -ansi -pedantic yields:
test.cc:7: error: call of overloaded 'f()' is ambiguous
test.cc:1: note: candidates are: void f() [with T = int]
test.cc:2: note: int f() [with T = int]
There is no way to call f.
> ,
> if 13/1 is meant to apply to these, ignoring the surrounding template
> clause. If not, then wouldn't it forbid overloading function templates
> altogether, contradicting 14.5.5 which does allow it?
>
> Isn't "function declaration" a semantical property of a declaration, which
It's a syntactic property also; function declarations have a particular syntax.
> does not apply to containd declarations found in function template
> declarations, because such a declaration declares a template, but not a
> function?
A function template declaration declares a function template.
The embedded function syntax in that declaration gives important
semantic properties which are attached to the function template,
such as its name, return type, argument signature, and possibly
a body if it is a definition. And also the overall category that it's
a function template rather than a class template!
I.e. a template is not just a textual macro:
template<args, ...> arbitrary tokens to be collected and parsed later
But templates do have some aspects of a macro; some kinds of analysis
which takes place right away on non-template entities is, for templates,
deferred until instantiation time.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Wed, 11 Nov 2009 17:22:09 CST Raw View
Kaz Kylheku wrote:
> On 2009-11-10, Johannes Schaub (litb) <schaub-johannes@web.de> wrote:
>> Hello all. I'm confused by 13/1 in the c++0x draft and in c++03, which
>> says=
>>:
>>
>> "Only function declarations can be overloaded; object and type
>> declarations cannot be overloaded."
>>
>> But we can also overload function templates.
>
> The unqualified term function includes both template and non-template
> functions. In situations that apply to only one or the other, the text
> makes it clear.
>
> We can overload function templates, with other function templates as
> well as with non-template functions.
>
> This is given in 14.5.5 of c++03.
>
I see. Yeah sometimes I see how it applies to both, like with "member
function" applying to member function templates too. But i've seen texts
that reads like "functions, function templates, ...". I've read "call to a
function" as meaning "call to a function that's not a template
specialization, or call to a function template specialization" (the latter
being a function too), but read "function declaration" as explicitly only
refering to plain functions, not to function template declarations. Two
examples from 3.3/4 and 3.3.7/2:
"Given a set of declarations in a single declarative region, each of which
specifies the same unqualified name, they shall all refer to the same
entity, or all refer to functions and function templates;", in this case,
they are treated different, and I think also in the following - although
compilers disagree:
"A class name (9.1) or enumeration name (7.2) can be hidden by the name of
an object, function, or enumerator declared in the same scope."
I think the following is ill-formed, and comeau/edg agrees with me:
class F { };
template<typename T> void F();
It doesn't seem to be clear that "function" refers to both - GCC and clang
accept that code even when in pedantic mode.
> The rule in question seems to prohibit situations like this:
>
> template<typename T> void f();
> template<typename T> int f();
>
> These two differ ``only in the return type''. All else is the same: same
> argument signature, same template parameters.
>
> Implementation note: g++ 4.3.2 accepts the above declarations, without
> a diagnostic. This appears to be a small noncoformance. However, you get
> a diagnostic if you try to use f.
>
On the contrary, i've always thought of the above to be valid, because for
templates, their signature includes the return type (see 14.5.5.1/4.) I
wonder what is the purpose of having the signature of a template include the
return type, of it's not used in determining overloadability anyway?
> There is no unambiguous way to call a particular one of the two f's
> above: For the call
>
> f<int>()
>
> Regardless of what --std option, g++-4.3.2 -Wall -ansi -pedantic yields:
>
> test.cc:7: error: call of overloaded 'f()' is ambiguous
> test.cc:1: note: candidates are: void f() [with T = int]
> test.cc:2: note: int f() [with T = int]
>
> There is no way to call f.
>
I think you can call using an explicit target type:
((int(*)())f)();
>> ,
>> if 13/1 is meant to apply to these, ignoring the surrounding template
>> clause. If not, then wouldn't it forbid overloading function templates
>> altogether, contradicting 14.5.5 which does allow it?
>>
>> Isn't "function declaration" a semantical property of a declaration,
>> which
>
> It's a syntactic property also; function declarations have a particular
> syntax.
>
>> does not apply to containd declarations found in function template
>> declarations, because such a declaration declares a template, but not a
>> function?
>
> A function template declaration declares a function template.
> The embedded function syntax in that declaration gives important
> semantic properties which are attached to the function template,
> such as its name, return type, argument signature, and possibly
> a body if it is a definition. And also the overall category that it's
> a function template rather than a class template!
>
> I.e. a template is not just a textual macro:
>
> template<args, ...> arbitrary tokens to be collected and parsed later
>
> But templates do have some aspects of a macro; some kinds of analysis
> which takes place right away on non-template entities is, for templates,
> deferred until instantiation time.
>
I see. It seems to me though it's a rath big stretch to apply clause 13 to
function templates too. Not sure yet :(
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]