Templates Functions of Subtypes

T

tutmann

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) {
return 0;
}

int i = f(x1,x2);


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) {
return 0;
}

int k = f(xv1, xv2);

Instead I get:
error: no matching function for call to ‘f(std::vector<X<int>,

What is wrong here?

Thank you!

Henning
 
V

Victor Bazarov

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>,


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
 
H

Henning

Thank you.
That's called *a non-deducible context*.  You cannot ask the compiler to
match the template argument based on a member type.

What is the definition of a deducible context? I somewhat doubt, it
has to do with References being passed.
 
P

Paul Bibbings

Henning said:
Thank you.


What is the definition of a deducible context? I somewhat doubt, it
has to do with References being passed.

According to the Standard (C++0x) a *non*-deduced context is defined by
[temp.deduct.type] §14.8.2.4/4

"The nondeduced contexts are:

- The nested-name-specifier of a type that was specified using a
qualified-id.

- A type that is a template-id in which one or more of the
template-arguments is an expression that references a
template-parameter.

When a type name is specified in a way that includes a nondeduced
context, all of the types that comprise that type name are also
nondeduced."

In effect, a /deduced/ context is defined through not meeting the
criteria (above) of being a nondeduced context, and where, according to
[temp.deduct.type] §14.8.2.4/9:

"A template type argument T, a template template argument TT or a
template non-type argument i can be deduced if P and A have one of
the following forms:

T
cv-list T
T*
T&
T[integer-constant]
template-name<T> (where template-name refers to a class template)
type(*)(T)
T(*)()
T(*)(T)
T type::*
type T::*
T T::*
T (type::*)()
type (T::*)()
type (type::*)(T)
type (T::*)(T)
T (type::*)(T)
T (T::*)()
T (T::*)(T)
type
template-name<i> (where template-name refers to a class template)
TT<T>
TT<i>
TT<>

where (T) represents argument lists where at least one argument type
contains a T, and () represents argument lists where no parameter
contains a T. Similarly, <T> represents template argument lists where
at least one argument contains a T, <i> represents template argument
lists where at least one argument contains an i and <> represents
template argument lists where no argument contains a T or an i."

.... and then there are bits about composition of such types, etc.

Regards

Paul Bibbings
 
V

Victor Bazarov

Thank you.


What is the definition of a deducible context? I somewhat doubt, it
has to do with References being passed.

Probably not with reference, but with the fact that you're trying to get
the template argument from the type of a member, not the type itself.

The definition of non-deducible context is the context from which
something (like the template argument) cannot be deduced. The
definition of a deducible context is the reverse of that. If you would
like to see the list of deducible contexts, open the Standard and look
at [temp.deduct.type]/9. The exhaustive list is there. Any context not
in that list is non-deducible.

V
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top