Topic: overload resolution and member templates


Author: pratik khasnabis <pratik.khasnabis@tre.ntc.nokia.com>
Date: 2000/09/22
Raw View
Its recently mentioned in comp.lang.c++ that VC compiler doesn't yet supp=
ort partial
specialization of templates. Its a fault of the compiler.

Pratik

Abdel Meghdir wrote:

> It seems that the compiler prefere a function with an  exact match over=
 a
> template function. How ever  there is a strange behavior with VC 5 comp=
iler
> it seems to generate an error : ambiguous call to overloaded function  =
if
> the exact matching function is declared befor the  template function an=
d it
> does not generate any errors when the template function is declared bef=
or
> the the exact type matching function .
>
> is there any reason behind this?
>
> here is the two versions of  the test I have done with VC++ 5.0
>
>  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  =3D "hello";
>    obj.foo(s);   //  compiler error :  ambiguous call to overloaded fun=
ction
>    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  =3D "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;
> }
>
> James Kuyper <kuyper@wizard.net> a =E9crit dans le message : 39C2AACB.1=
166F599@wizard.net...
> > vvinogra@speakeasy.org wrote:
> > >
> > > Please, consider the following code:
> > >
> > > class MyClass
> > > {
> > > public:
> > >
> > >         void foo(const char* arg)
> > >         {
> > >                 ...
> > >         }
> > >
> > >         template<typename T> void foo(T arg)
> > >         {
> > >                 ...
> > >         }
> > > };
> > >
> > > int main()
> > > {
> > >         MyClass obj;
> > >
> > >         obj.foo(1); // case 1
> > >         obj
> .foo("hello"); // case2
> > >
> > >         return 0;
> > > }
> > >
> > > What version of MyClass::foo() is going to be invoked in each case
> > > according to standard?
> >
> > There's nothing special about member functions in this regard; if the
> > two foo's were non-member functions you'd involve the same issues. Al=
l
> > else being equal, a function that's an exact match is preferred over =
a
> > template function that can be made an exact match by deducing the rig=
ht
> > type for the template argument. See 13.3.3p1.
> >
> > ---
> > [ comp.std.c++ is moderated.  To submit articles, try just posting wi=
th ]
> > [ 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           =
   ]
> >
>
> ---
> [ 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             =
 ]


=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D MODERATOR'S COMMENT:=20
 Please don't overquote.

---
[ 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              ]






Author: "Abdel Meghdir" <kmeghdir@yahoo.com>
Date: 2000/09/18
Raw View
It seems that the compiler prefere a function with an  exact match over a
template function. How ever  there is a strange behavior with VC 5 compiler
it seems to generate an error : ambiguous call to overloaded function  if
the exact matching function is declared befor the  template function and it
does not generate any errors when the template function is declared befor
the the exact type matching function .

is there any reason behind this?

here is the two versions of  the test I have done with VC++ 5.0

 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
   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;
}


James Kuyper <kuyper@wizard.net> a    crit dans le message : 39C2AACB.1166F599@wizard.net...
> vvinogra@speakeasy.org wrote:
> >
> > Please, consider the following code:
> >
> > class MyClass
> > {
> > public:
> >
> >         void foo(const char* arg)
> >         {
> >                 ...
> >         }
> >
> >         template<typename T> void foo(T arg)
> >         {
> >                 ...
> >         }
> > };
> >
> > int main()
> > {
> >         MyClass obj;
> >
> >         obj.foo(1); // case 1
> >         obj
.foo("hello"); // case2
> >
> >         return 0;
> > }
> >
> > What version of MyClass::foo() is going to be invoked in each case
> > according to standard?
>
> There's nothing special about member functions in this regard; if the
> two foo's were non-member functions you'd involve the same issues. All
> else being equal, a function that's an exact match is preferred over a
> template function that can be made an exact match by deducing the right
> type for the template argument. See 13.3.3p1.
>
> ---
> [ 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              ]
>


---
[ 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              ]






Author: vvinogra@speakeasy.org
Date: 2000/09/15
Raw View
Please, consider the following code:

class MyClass
{
public:

 void foo(const char* arg)
 {
  ...
 }

 template<typename T> void foo(T arg)
 {
  ...
 }
};

int main()
{
 MyClass obj;

 obj.foo(1); // case 1
 obj.foo("hello"); // case2

 return 0;
}

What version of MyClass::foo() is going to be invoked in each case
according to standard?

Thanks,

Vlad


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ 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              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 2000/09/15
Raw View
vvinogra@speakeasy.org wrote:
>
> Please, consider the following code:
>
> class MyClass
> {
> public:
>
>         void foo(const char* arg)
>         {
>                 ...
>         }
>
>         template<typename T> void foo(T arg)
>         {
>                 ...
>         }
> };
>
> int main()
> {
>         MyClass obj;
>
>         obj.foo(1); // case 1
>         obj.foo("hello"); // case2
>
>         return 0;
> }
>
> What version of MyClass::foo() is going to be invoked in each case
> according to standard?

There's nothing special about member functions in this regard; if the
two foo's were non-member functions you'd involve the same issues. All
else being equal, a function that's an exact match is preferred over a
template function that can be made an exact match by deducing the right
type for the template argument. See 13.3.3p1.

---
[ 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              ]