Implied template parameters.

J

jason.cipriani

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
 
J

James Kanze

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.
 
J

jason.cipriani

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top