Re: Templates Functions of Subtypes
On 6/17/2010 10:43 AM, tutmann wrote:
Hello All,
I'm confused about templating functions.
Say I have this, which is working fine:
#include<vector>
template<typename T>
struct X {
typedef std::vector< X< T> > Vector;
T x;
};
typedef X< int> XI;
XI x1, x2;
template<typename T>
int f(X<T> x1, X<T> x2) {
Note that the arguments aren't references.
return 0;
}
int i = f(x1,x2);
T is *deducible* here. From the type of 'x1' to the type of the
argument - no problem.
Then, why isn't that working as well?
XI::Vector xv1, xv2;
template<typename T>
int f(typename X<T>::Vector&x1, typename X<T>::Vector&x2) {
Note that 'x1' and 'x2' are declared *references*, and not to an
instantiation of the X template, but to the inner type declared in X.
return 0;
}
int k = f(xv1, xv2);
Instead I get:
error: no matching function for call to ?f(std::vector<X<int>,
std::allocator<X<int> > >&, std::vector<X<int>, std::allocator<X<int>
What is wrong here?
That's called *a non-deducible context*. You cannot ask the compiler to
match the template argument based on a member type.
V
--
I do not respond to top-posted replies, please don't ask