Topic: VC++ & overloading


Author: "Abdel Meghdir" <kmeghdir@yahoo.com>
Date: 2000/09/21
Raw View
For  overloading  resolution the compiler  seems to prefere  functions with
no type convertions of  arguments
over  functions wich needs arguments type convertions. ( if I get it
right ).  How ever there is some thing strange with VC 5 compiler.    The
order of definition of  template functions  seems  to be determinent .


Here 2 exemples :  in the first one , it generate a compile : error
ambiguous call to overloaded function .
and  there is no error in the second one .
the only difference beteween the first and the second one is the order of
definition of  the template function.

is there any reason behind this?


 class MyClass
 {
  public:
    void foo(const char* arg)
   {
       cout << " function with exact matching type arguments "<<endl;
    }
   template<typename T> void foo(T arg)
   {
       cout <<"template function prefered: "<<endl;
   }
};
int main()
{
   MyClass obj;
   const char*   s  = "hello";
   obj.foo(s);   //  compiler error :  ambiguous call to overloaded function
   obj.foo(1);
   return 0;
}
    ------------------------------------------------
class MyClass
 {
  public:
   template<typename T> void foo(T arg)
   {
       cout <<"template function called: "<<endl;
   }
   void foo(const char* arg)
   {
       cout << "Function with exact matching type arguments is called"<<endl;
   }

};
int main()
{
   MyClass obj;
   const char*   s  = "hello";
   obj.foo(s);   //  no error :   the Function with exact matching type arguments is called
   obj.foo(1);  //  no error :   the template function is called
   return 0;
}




---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]