I have an overloaded function that works correctly. (f in the example). When I convert it to a template version of the same, it is interrupted, always calling T & versions, never T *. (t in the example) When I create a non-constant version of the template function, it works as expected. (t2 in the example) This happens both in VS2010 and in g ++ 4.6.2. Are the rewards for constant rules different, or is this some kind of mistake.
#include <iostream> using namespace std; int f(const int&x){return 1;} int f(const int*x){return 2;} template <class T> int t(const T &x){return 3;} template <class T> int t(const T *x){return 4;} template <class T> int t2(T &x){return 5;} template <class T> int t2(T *x){return 6;} int main(int argc, char ** argv){ int x=0; cout<<f(x)<<endl; cout<<f(&x)<<endl; cout<<t(x)<<endl; cout<<t(&x)<<endl; cout<<t2(x)<<endl; cout<<t2(&x)<<endl; return 0; }
output
1 2 3 3 5 6
source share