|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|