Connecting Tech Pros Worldwide Forums | Help | Site Map

overloading

kiryazev@gmail.com
Guest
 
Posts: n/a
#1: Dec 7 '06
Hello. Does anyone have any idea why is the template function choosed
by the compiler in the following code?

#include <iostream>

template<class T>
void f(T)
{
std::cout << "#1\n";
}

void f(char*)
{
std::cout << "#2\n";
}

int main()
{
f("test"); // #1
}


dasjotre
Guest
 
Posts: n/a
#2: Dec 7 '06

re: overloading



kiryazev@gmail.com wrote:
Quote:
Hello. Does anyone have any idea why is the template function choosed
by the compiler in the following code?
>
#include <iostream>
>
template<class T>
void f(T)
{
std::cout << "#1\n";
}
>
void f(char*)
{
std::cout << "#2\n";
}
>
int main()
{
f("test"); // #1
}
In f("test")

the char pointer bound to the string is a non named temporary which can
be bound to const reference only.

Try this

void f(char const *)
{
std::cout <<"f(char const *)\n";
}

char * p = "test";
f(p);
f("test");

Salt_Peter
Guest
 
Posts: n/a
#3: Dec 7 '06

re: overloading



kiryazev@gmail.com wrote:
Quote:
Hello. Does anyone have any idea why is the template function choosed
by the compiler in the following code?
>
#include <iostream>
>
template<class T>
void f(T)
{
std::cout << "#1\n";
}
>
void f(char*)
{
std::cout << "#2\n";
}
>
int main()
{
f("test"); // #1
}
The literal string is a const pointer to a sequence of const chars, so:

#include <iostream>

template<class T>
void f(const T* const)
{
std::cout << "#1\n";

}

void f(const char* const)
{
std::cout << "#2\n";

}

int main()
{
f("test");
}

/*
#2
*/

Steven T. Hatton
Guest
 
Posts: n/a
#4: Dec 7 '06

re: overloading


Salt_Peter wrote:

Quote:
The literal string is a const pointer to a sequence of const chars, so:
>
#include <iostream>
>
template<class T>
void f(const T* const)
{
std::cout << "#1\n";
>
}
>
void f(const char* const)
{
std::cout << "#2\n";
>
}
>
int main()
{
f("test");
}
>
/*
#2
*/
Rather than start a thread I'm gonna segue off this one. _C++ Templates:
The Complete Guide_ provides a list of criteria for determining the
signature of a function or function template. A footnote acknowledges that
their list is distinct from what the Standard provides, but claims it is
functionally equivalent. One criterion the authors of the book list is
return type. I have been given to believe that return type is signally
absent from the criteria determining signature. Am I mistaken?
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Micah Cowan
Guest
 
Posts: n/a
#5: Dec 8 '06

re: overloading


Salt_Peter wrote:
Quote:
The literal string is a const pointer to a sequence of const chars, so:
>
#include <iostream>
....
Quote:
void f(const char* const)
{
std::cout << "#2\n";
>
}
What in the world gave you that idea? A string literal is an /array/ of
N const char, which will in most cases be implicitly converted to a
(non-const) pointer to const char.

Micah Cowan
Guest
 
Posts: n/a
#6: Dec 8 '06

re: overloading


Steven T. Hatton wrote:
Quote:
Rather than start a thread I'm gonna segue off this one. _C++ Templates:
The Complete Guide_ provides a list of criteria for determining the
signature of a function or function template. A footnote acknowledges that
their list is distinct from what the Standard provides, but claims it is
functionally equivalent. One criterion the authors of the book list is
return type. I have been given to believe that return type is signally
absent from the criteria determining signature. Am I mistaken?
Actually, you'd have been better off starting a distinct thread,
probably: anyone who decides they aren't interested in the original
thread might set their reader to ignore that thread, and thus would
miss out on your new question.

To answer your question: you are correct... _when_ you're not talking
about function templates. From the standard:

13.3#2:
When an overloaded function name is used in a call, which overloaded
function declaration is being referenced is determined by comparing the
types of the arguments at the point of use with the types of the
parameters in the overloaded declarations that are visible at the point
of use.

Note that there is no mention of return types. The section that gives
more details on this mechanism also corroborates this text, as does
13.3.1#2, which specifies that functions differing only in return type
may not be overloaded (and thus, any program which declares such
functions in the same scope is ill-formed).

However:

14.5.5.1#4:
The signature of a function template consists of its function
signature, its return type and its template parameter list.

This does /not/ mean, however, that a compiler can somehow determine
which function template to use when two such templates differ only in
return type. Actually, after staring at the relevant portions of the
Standard for a while, I'm not /entirely/ sure what it /does/ mean;
it'll take some more poring over (or a more knowledgable poster) to
clear that up for me. After all, the definition of a signature is "the
information about a function that participates in overload resolution";
but what role it plays in resolving overloads is not yet clear to me.

Salt_Peter
Guest
 
Posts: n/a
#7: Dec 8 '06

re: overloading



Micah Cowan wrote:
Quote:
Salt_Peter wrote:
>
Quote:
The literal string is a const pointer to a sequence of const chars, so:

#include <iostream>
>
...
>
Quote:
void f(const char* const)
{
std::cout << "#2\n";

}
>
What in the world gave you that idea? A string literal is an /array/ of
N const char, which will in most cases be implicitly converted to a
(non-const) pointer to const char.
And, whats your point?

Chris Theis
Guest
 
Posts: n/a
#8: Dec 10 '06

re: overloading



"Steven T. Hatton" <chattengau@germania.supwrote in message
news:H8ydnflRxeAUqOXYnZ2dnUVZ_vamnZ2d@speakeasy.ne t...

[SNIP]
Quote:
Rather than start a thread I'm gonna segue off this one. _C++ Templates:
The Complete Guide_ provides a list of criteria for determining the
signature of a function or function template. A footnote acknowledges
that
their list is distinct from what the Standard provides, but claims it is
functionally equivalent. One criterion the authors of the book list is
return type. I have been given to believe that return type is signally
absent from the criteria determining signature. Am I mistaken?
In that case you are mistaken. According to the standard (ISO 14882:2003(E)
chapter 14.5.5.1 clause 4) the signature of a function template consists of
its function signature, its return type and its template parameter list.

HTH
Chris


Closed Thread