From: junvi on 10 Aug 2010 04:41 #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 -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: CornedBee on 11 Aug 2010 14:24 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 Both plain f and f<const int*> are perfect matches for the given argument. In this case, the standard specifies that the non-template is to be preferred. This allows non-templates to override templates for specific cases. Sebastian -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Daniel Krügler on 11 Aug 2010 14:24 On 10 Aug., 21:41, 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 The template version is no perfect match. If you want to make it a better match, you better declare it as template <typename T> void f(T* a) { ... } If a function template and a non-template function equally compete, the non-template function is preferred over the template. This means that in the slightly changed definition template <typename T> void f(const T* a) { ... } the non-template function will be chosen instead. HTH & Greetings from Bremen, Daniel Kr�gler -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Florian on 11 Aug 2010 16:03 On 10 ao�t, 21:41, 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 } With gcc4.5, the output is Template Version, and it make me in trouble, in my mind it could be Plain Version. I thought compiler check declared functions (so no template) and implicit conversion before try to match template, but it seems to be little different : it try to match template declaration before use implicit conversion. const T (with T = int*) = int* const, not const int*, so the template don't need conversion, so it's using. If you type int* cont instead of const int*, it's the plain version witch is using. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Hakusa on 11 Aug 2010 16:01
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 This is my run of your code: $ g++ main.cpp -o run $ ./run Template version. $ g++ --version g++ (GCC) 4.4.4 20100630 (Red Hat 4.4.4-10) -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |