I have problems understanding how overloading of function templates
works.
Consider first the following code without any function templates ...
int foo(const char *& c)
{
return 0;
}
int foo(const char *c)
{
return 1;
}
int main() {
const char * s = "Hi!";
return foo(s);
}
Overloading resolution of foo is ambiguous, as can be witness by e.g. g
++
Foo.cpp: In function 'int main()':
Foo.cpp:14: error: call of overloaded 'foo(const char*&)' is ambiguous
Foo.cpp:2: note: candidates are: int foo(const char*&)
Foo.cpp:7: note: int foo(const char*)
If I now use function templates for foo, foo_t, as follows
template<typename C>
int foo_t(C& c)
{
return 0;
}
template<typename C>
int foo_t(const C *c)
{
return 1;
}
int main() {
const char * s = "Hi!";
return foo_t(s);
}
some implementations I tested (g++, msvc) seem to find a best
candidate function for foo_i which I do not understand.
As I read 13.3.1 para 7 of the standard, I would expect an
implementation to first determine specializations for foo_t (here:
foo_t<const char *and foo_t<charresp.), and then proceed as above.
Hence, I'd also expect an ambiguity error here. Where is my
misunderstanding?
Cheers,
Matthias 7 2297
On Oct 3, 10:07 am, matthias.neuba...@gmail.com wrote:
I have problems understanding how overloading of function templates
works.
Consider first the following code without any function templates ...
int foo(const char *& c)
{
return 0;
}
int foo(const char *c)
{
return 1;
}
int main() {
const char * s = "Hi!";
return foo(s);
}
template<typename C>
int foo_t(C& c)
{
return 0;
}
template<typename C>
int foo_t(const C *c)
{
return 1;
}
int main() {
const char * s = "Hi!";
return foo_t(s);
}
some implementations I tested (g++, msvc) seem to find a best
candidate function for foo_i which I do not understand.
As I read 13.3.1 para 7 of the standard, I would expect an
implementation to first determine specializations for foo_t (here:
foo_t<const char *and foo_t<charresp.), and then proceed as above.
Hence, I'd also expect an ambiguity error here. Where is my
misunderstanding?
The specializations are determined but now your function arguments
have changed. The two methods become:
int foot_t(const char*&)
and
int foo_t(const char**)
So if you're passing in a const char* my expectation would be the
foo_t(const char*&) method is called
mojumbo wrote:
On Oct 3, 10:07 am, matthias.neuba...@gmail.com wrote:
>I have problems understanding how overloading of function templates works.
Consider first the following code without any function templates ...
int foo(const char *& c) { return 0; }
int foo(const char *c) { return 1; }
int main() { const char * s = "Hi!"; return foo(s); }
template<typename C> int foo_t(C& c) { return 0; }
template<typename C> int foo_t(const C *c) { return 1; }
int main() { const char * s = "Hi!"; return foo_t(s); }
some implementations I tested (g++, msvc) seem to find a best candidate function for foo_i which I do not understand.
As I read 13.3.1 para 7 of the standard, I would expect an implementation to first determine specializations for foo_t (here: foo_t<const char *and foo_t<charresp.), and then proceed as above. Hence, I'd also expect an ambiguity error here. Where is my misunderstanding?
The specializations are determined but now your function arguments
have changed. The two methods become:
int foot_t(const char*&)
and
int foo_t(const char**)
Why? foo_t(const char**) wouldn't fit, and it wouldn't even be a possible
template instance (what would the template argument C be?).
But foo_t(const char*) would fit (with C=char). And actually, my compiler is
choosing that one.
On Oct 3, 3:07 pm, matthias.neuba...@gmail.com wrote:
I have problems understanding how overloading of function templates
works.
[...]
As I read 13.3.1 para 7 of the standard, I would expect an
implementation to first determine specializations for foo_t (here:
foo_t<const char *and foo_t<charresp.), and then proceed as above.
Hence, I'd also expect an ambiguity error here. Where is my
misunderstanding?
When function templates are involved then selection is based on which
candidate is more specialized. See "13.3.3 Best Viable Function" and
"14.5.6.2 Partial ordering of function templates" (Draft C++ Standard,
N2723).
For example,
template <class Tvoid foo (T a); // #1
template <class Tvoid foo (T* a); // #2, more specialized
int* a; foo (a); // selects #2
Regards,
Vidar Hasfjord
On Oct 3, 5:50*pm, Vidar Hasfjord <vattilah-gro...@yahoo.co.ukwrote:
When function templates are involved then selection is based on which
candidate is more specialized. See "13.3.3 Best Viable Function" and
"14.5.6.2 Partial ordering of function templates" (Draft C++ Standard,
N2723).
Ok, now I see. [over.match.best] compares function template
specializations differently. That's the part I was missing. Thanks!
On Fri, 03 Oct 2008 16:07:46 +0200, <ma***************@gmail.comwrote:
I have problems understanding how overloading of function templates
works.
.... snip ...
If I now use function templates for foo, foo_t, as follows
template<typename C>
int foo_t(C& c)
{
return 0;
}
template<typename C>
int foo_t(const C *c)
{
return 1;
}
int main() {
const char * s = "Hi!";
return foo_t(s);
}
some implementations I tested (g++, msvc) seem to find a best
candidate function for foo_i which I do not understand.
As I read 13.3.1 para 7 of the standard, I would expect an
implementation to first determine specializations for foo_t (here:
foo_t<const char *and foo_t<charresp.), and then proceed as above.
Hence, I'd also expect an ambiguity error here. Where is my
misunderstanding?
The reason why there is no ambiguity, is because the two functions are
ordered by the partial ordering of functions. That is, the second template
is more specialized than the first. That is possible because, when
matching (const C*) against C&, the reference is stripped from C& (see
14.8.2.1/2). If you call foo_t(1) [eg. calling the C& version with an
rvalue], overload resolution succeeds, but the call fails.
Regards
Jiri Palecek
On Oct 3, 7:42 pm, Jiří Paleček <jpale...@web.dewrote:
On Fri, 03 Oct 2008 16:07:46 +0200, <matthias.neuba...@gmail.comwrote:
template<typename C>
int foo_t(C& c)
{
return 0;
}
template<typename C>
int foo_t(const C *c)
{
return 1;
}
int main() {
const char * s = "Hi!";
return foo_t(s);
}
The reason why there is no ambiguity, is because the two functions are
ordered by the partial ordering of functions. That is, the second template
is more specialized than the first. That is possible because, when
matching (const C*) against C&, the reference is stripped from C& (see
14.8.2.1/2). If you call foo_t(1) [eg. calling the C& version with an
rvalue], overload resolution succeeds, but the call fails.
ALright. I am still a little bit confused. As you say, when comparing
function templates ("14.5.5.2 partial ordering of function
templates"), we perform argument deduction. But 14.5.5.2p4 also
dictates
"The transformed template is at least as specialized as the other
if, and only if, the deduction succeeds
and the deduced parameter types are an exact match (so the
deduction does not rely on implicit conversions)."
What exactly does the term "exact match" mean in this context? Is this
specified somewhere else?
In my example above, as I understand it, the deduced parameter type
for (C&) is
const C*&
which is NOT an exact match to
const C*.
Instead, we MUST use an implicit conversion to strip away the
reference.
Or, does the term "exact match" mean here, we still compare the
deduced parameter type and the expected parameter type "inexactly" by
ignoring
some parts as specified in 14.8.2.1?
-Matthias
On 7 Okt, 12:45, matthias.neuba...@gmail.com wrote:
On Oct 3, 7:42 pm, Jiří Paleček <jpale...@web.dewrote:
On Fri, 03 Oct 2008 16:07:46 +0200, <matthias.neuba...@gmail.comwrote:
* template<typename C>
* int foo_t(C& c)
* {
* *return 0;
* }
* template<typename C>
* int foo_t(const C *c)
* {
* *return 1;
* }
* int main() {
* * const char * s = "Hi!";
* * return foo_t(s);
* }
The reason why there is no ambiguity, is because the two functions are
ordered by the partial ordering of functions. That is, the second template
is more specialized than the first. That is possible because, when
matching (const C*) against C&, the reference is stripped from C& (see
14.8.2.1/2). If you call foo_t(1) [eg. calling the C& version with an
rvalue], overload resolution succeeds, but the call fails.
ALright. I am still a little bit confused. As you say, when comparing
function templates ("14.5.5.2 partial ordering of function
templates"), we perform argument deduction. But 14.5.5.2p4 also
dictates
* "The transformed template is at least as specialized as the other
if, and only if, the deduction succeeds
* *and the deduced parameter types are an exact match (so the
deduction does not rely on implicit conversions)."
What exactly does the term "exact match" mean in this context? Is this
specified somewhere else?
In my example above, as I understand it, the deduced parameter type
for (C&) is
* *const C*&
which is NOT an exact match to
* *const C*.
Instead, we MUST use an implicit conversion to strip away the
reference.
Or, does the term "exact match" mean here, we still compare the
deduced parameter type and the expected parameter type "inexactly" by
ignoring
some parts as specified in 14.8.2.1?
-Matthias
Yes, there is a special case for reference binding, so that the
conversion from const C* to const C*& has "Exact Match" ranking. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dave Theese |
last post by:
Hello all,
I'm trying to get a grasp of the difference between specializing a function
template and overloading it. The example below has a primary template, a
specialization and an overload. ...
|
by: al |
last post by:
1. If a function template is defined:
template <typename A>
A SQ(A& a)
{
return a*a;
}
How could prevent users from passing a type which makes no sense, like
Employee class type?
|
by: Richard Hayden |
last post by:
Hi,
I have the following code for example:
/**********************************/
#include <iostream>
class C1 {
public:
|
by: WittyGuy |
last post by:
Hi,
What is the major difference between function overloading and function
templates?
Thanks!
http://www.gotw.ca/resources/clcm.htm for info about ]
|
by: Eph0nk |
last post by:
Hi,
I get an overload resolution failed error (Overload resolution failed because no accessible 'New' can be called without a narrowing conversion), and I can't seem to find a lot of relevant...
|
by: bcobra |
last post by:
What am I doing wrong?
the code:
age Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1"
%>
<%@ Register TagPrefix="MM" Namespace="DreamweaverCtrls"...
|
by: hurcan solter |
last post by:
given the code snippet;
template<typename T>
void foo(T,T){}
template<typename T1,typename T2>
void foo(T1*,T2*){}
int main( ) {
|
by: xtrigger303 |
last post by:
Hi to all,
I was reading Mr. Alexandrescu's mojo article and I've a hard time
understanding the following.
Let's suppose I have:
//code
struct A {};
struct B : A {};
|
by: zbigniew |
last post by:
Can someone explain me how overload resolution works in C++?
For example if I have function void f(char, int); and I will call
f('A', 3.1) or f(1.5, 3.1F) what would be the result?
Thanks
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |