From: Ike Naar on
In article <48423290-31d8-4e62-bdd1-9db86fa61033(a)v15g2000yqe.googlegroups.com>,
junvi <junvi.byhh(a)gmail.com> wrote:
> [...]
>template <typename T>
>void f(const T a) {
> cout<<"Template version."<<endl;
>}
>void f(const int* a) {
> cout<<"Plain version."<<endl;
>}
>int main() {
> int *a=0;
> f(a);
> return 0;
>}
>
>the output is Plain version., I don't understand why is the case

Calling plain ``f'' with ``a'' as an argument requires an implicit
conversion from ``int*'' to ``const int*'', while no conversion
is required for the templated f, so templated f is a better match.

Change the declaration of ``a'' in main to ``const int *a = 0;''
then plain ``f'' will be chosen.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Jia-sen on
On Aug 10, 3:41 pm, junvi <junvi.b...(a)gmail.com> wrote:
> #include <iostream>
> using namespace std;
>
> template <typename T>
> void f(const T a) {
> cout<<"Template version."<<endl;
>
> }
>
> void f(const int* a) {
> cout<<"Plain version."<<endl;
>
> }
>
> int main() {
>
> int *a=0;
>
> f(a);
>
> return 0;
>
> }
>
> the output is Plain version., I don't understand why is the case

{ quoted clc++m banner removed; please do it yourself. -mod }

The point is that non-const to const conversion is still a conversion.
The argument 'a' is non-const, so template version matches better than
the plain one. Try define 'a' as 'const int* a=0' and see which
version is called.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: red floyd on
On Aug 10, 12:41 pm, junvi <junvi.b...(a)gmail.com> wrote:
> #include <iostream>
> using namespace std;
>
> template <typename T>
> void f(const T a) {
> cout<<"Template version."<<endl;
>
> }
>
> void f(const int* a) {
> cout<<"Plain version."<<endl;
>
> }
>
> int main() {
>
> int *a=0;
>
> f(a);
>
> return 0;
>
> }
>
> the output is Plain version., I don't understand why is the case

Your question doesn't make sense. You ask "Why template version is
preferred". it's not. You get the non-template version, as you
should.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Christian Broom on
The code doesn't make much sense in general. As is, the specification
says that a pointer to null results in undefined behavior. My compiler
(VC++08) calls the template function instead. To be sure which
function you call, give 'a' a proper reference and use the form:

f<T>(a) // Specifically call the template function with a type T.

To be really sure which function is called, don't be silly enough to
declare two functions with the same name. It is usually a sign of bad
design and merits refactoring.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Florian on
On 12 ao�t, 15:15, Christian Broom <soulbea...(a)gmail.com> wrote:
> The code doesn't make much sense in general. As is, the specification
> says that a pointer to null results in undefined behavior. My compiler
> (VC++08) calls the template function instead. To be sure which
> function you call, give 'a' a proper reference and use the form:

Use a NULL pointer is a UB, but here the pointer isn't use (juste
define), so there isn't UB, just normal behavior.

> To be really sure which function is called, don't be silly enough to
> declare two functions with the same name. It is usually a sign of bad
> design and merits refactoring.

Overload template function is a good solution to solve the problem of
partial specialization, so have two functions with the same name could
be good (and I thing that overload is better than specialization for
template function, but I don't find a good source which explains that,
perhaps the book of Alexandrescu and Sutter).


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]