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 > Implied template parameters.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Implied template parameters.

Réponse
 
LinkBack Outils de la discussion
Vieux 05/06/2008, 02h25   #1
jason.cipriani@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Implied template parameters.

In this code:

===

template <int N> class Base { };

template <int N> class Other {
public:
explicit Other (Base<N> &) { }
};

int main () {
Base<3> ok;
Other<3> fine(ok);
}

===

Maybe this is a dumb question, but why do I have to specify the
template parameter to "Other<3> fine(ok)"? Can't it deduce that it
should be a 3 on it's own (since a Base<3> is being passed to the
constructor, and Base<3> has no casting operators that could confuse
it, there's no other type it could be)? I want to just say "Other
fine(ok);".

Jason
  Réponse avec citation
Vieux 05/06/2008, 10h17   #2
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Implied template parameters.

On Jun 5, 3:25 am, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.com> wrote:
> In this code:


> ===


> template <int N> class Base { };


> template <int N> class Other {
> public:
> explicit Other (Base<N> &) { }
> };


> int main () {
> Base<3> ok;
> Other<3> fine(ok);
> }


> ===


> Maybe this is a dumb question, but why do I have to specify
> the template parameter to "Other<3> fine(ok)"? Can't it deduce
> that it should be a 3 on it's own (since a Base<3> is being
> passed to the constructor, and Base<3> has no casting
> operators that could confuse it, there's no other type it
> could be)? I want to just say "Other fine(ok);".


The simple answer is: because the standard says so. Template
type deduction only works for function templates. Other is a
class template. Basically, in this case, the compiler must
first know the type of Other, in order to know where it should
look for the constructors. It can't do it in the reverse order.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Vieux 08/06/2008, 20h24   #3
jason.cipriani@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Implied template parameters.

On Jun 5, 5:17 am, James Kanze <james.ka...@gmail.com> wrote:
> On Jun 5, 3:25 am, "jason.cipri...@gmail.com"
> <jason.cipri...@gmail.com> wrote:
> > In this code:
> > ===
> > template <int N> class Base { };
> > template <int N> class Other {
> > public:
> > explicit Other (Base<N> &) { }
> > };
> > int main () {
> > Base<3> ok;
> > Other<3> fine(ok);
> > }
> > ===
> > Maybe this is a dumb question, but why do I have to specify
> > the template parameter to "Other<3> fine(ok)"? Can't it deduce
> > that it should be a 3 on it's own (since a Base<3> is being
> > passed to the constructor, and Base<3> has no casting
> > operators that could confuse it, there's no other type it
> > could be)? I want to just say "Other fine(ok);".

>
> The simple answer is: because the standard says so. Template
> type deduction only works for function templates. Other is a
> class template. Basically, in this case, the compiler must
> first know the type of Other, in order to know where it should
> look for the constructors. It can't do it in the reverse order.


That makes sense.

Thanks for clearing that up,
Jason
  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 13h43.


É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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13185 seconds with 11 queries