PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > ambiguous call to overloaded template function
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
ambiguous call to overloaded template function

Réponse
 
LinkBack Outils de la discussion
Vieux 30/06/2008, 08h33   #1
Ruki
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut ambiguous call to overloaded template function

I want to overload function to swap two others types, for example,
type<T>, T* and so on,
but I can't compile the following code at VC 6.0.

The compiler says :
error C2667: 'swap' : none of 2 overload have a best conversion
error C2668: 'swap' : ambiguous call to overloaded function
-------------------------------------------------------------------------------------------------------------
template < typename T >
void swap(T &lhs, T &rhs)
{
// do some thing
}

template < typename T >
void swap(T *lhs, T *rhs)
{
// do some thing
}

template < typename T >
struct type{};

template < typename T >
void swap(type<T> &lhs, type<T> &rhs)
{
// do some thing
}
int main()
{
int *p1,*p2;
type<int> t1, t2;
swap(t1, t2);
swap(p1, p2);

  Réponse avec citation
Vieux 30/06/2008, 08h40   #2
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ambiguous call to overloaded template function

* Ruki:
> I want to overload function to swap two others types, for example,
> type<T>, T* and so on,
> but I can't compile the following code at VC 6.0.
>
> The compiler says :
> error C2667: 'swap' : none of 2 overload have a best conversion
> error C2668: 'swap' : ambiguous call to overloaded function
> -------------------------------------------------------------------------------------------------------------
> template < typename T >
> void swap(T &lhs, T &rhs)
> {
> // do some thing
> }
>
> template < typename T >
> void swap(T *lhs, T *rhs)
> {
> // do some thing
> }
>
> template < typename T >
> struct type{};
>
> template < typename T >
> void swap(type<T> &lhs, type<T> &rhs)
> {
> // do some thing
> }
> int main()
> {
> int *p1,*p2;
> type<int> t1, t2;
> swap(t1, t2);
> swap(p1, p2);



Compiles fine with two modern compilers.

Don't use VC6, it's old.

Don't use the name 'swap', it's used by the standard library.

Don't try to be too subtle, like relying on subtleties of overload resolution.

Why don't you just use std::swap instead of defining your own?


Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  Réponse avec citation
Vieux 30/06/2008, 09h00   #3
Ruki
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ambiguous call to overloaded template function


> Alf P. Steinbach:


>Don't use VC6, it's old.

I need compiler compatibility

>Why don't you just use std::swap instead of defining your own?

I also need use std::swap specialization for my own types even if i
use std::swap
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h47.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10494 seconds with 11 queries