A template can be a member of a structure, class, or template class. The STL requires this feature to fully implement its design. Listing 14.20 provides a short example of a template class with a nested template class and a template function as members.
// tempmemb.cpp -- template members
#include <iostream>
using std::cout;
using std::endl;
template <typename T>
class beta
{
private:
template <typename V> // nested template class member
class hold
{
private:
V val;
public:
hold(V v = 0) : val(v) {}
void show() const { cout << val << endl; }
V Value() const { return val; }
};
hold<T> q; // template object
hold<int> n; // template object
public:
beta( T t, int i) : q(t), n(i) {}
template<typename U> // template method
U blab(U u, T t) { return (n.Value() + q.Value()) * u / t; }
void Show() const { q.show(); n.show();}
};
int main()
{
beta<double> guy(3.5, 3);
cout << "T was set to double
";
guy.Show();
cout << "V was set to T, which is double, then V was set to int
";
cout << guy.blab(10, 2.3) << endl;
cout << "U was set to int
";
cout << guy.blab(10.0, 2.3) << endl;
cout << "U was set to double
";
cout << "Done
";
return 0;
}
The hold
template is declared in the private section in Listing 14.20, so it is accessible only within the beta
class scope. The beta
class uses the hold
template to declare two data members:
hold<T> q; // template object
hold<int> n; // template object
n
is a hold
object based on the int
type, and the q
member is a hold
object based on the T
type (the beta
template parameter). In main()
, the following declaration makes T
represent double
, making q
type hold<double>
:
beta<double> guy(3.5, 3);
The blab()
method has one type (U
) that is determined implicitly by the argument value when the method is called and one type (T
) that is determined by the instantiation type of the object. In this example, the declaration for guy
sets T
to type double
, and the first argument in the method call in the following sets U
to type int
, matching the value 10
:
cout << guy.blab(10, 2.5) << endl;
Thus, although the automatic type conversions brought about by mixed types cause the calculation in blab()
to be done as type double
, the return value, being type U
, is an int
. Hence, it is truncated to 28
, as the following program output shows:
T was set to double
3.5
3
V was set to T, which is double, then V was set to int
28
U was set to int
28.2609
U was set to double
Done
Note that replacing 10
with 10.0
in the call to guy.blab()
causes U
to be set to double
, making the return type double
, which is reflected in 28.2609
being displayed.
As mentioned previously, the type of the second parameter is set to double
by the declaration of the guy
object. Unlike the first parameter, then, the type of the second parameter is not set by the function call. For instance, the following statement would still implement blah()
as blah(int, double)
, and the 3
would be converted to type double
by the usual function prototype rules:
cout << guy.blab(10, 3) << endl;
You can declare the hold
class and blah
method in the beta
template and define them outside the beta
template. However, sufficiently old compilers won’t accept template members at all, and others that accept them as shown in Listing 14.20 don’t accept definitions outside the class. However, if your compiler is willing and able, here’s how defining the template methods outside the beta
template would look:
template <typename T>
class beta
{
private:
template <typename V> // declaration
class hold;
hold<T> q;
hold<int> n;
public:
beta( T t, int i) : q(t), n(i) {}
template<typename U> // declaration
U blab(U u, T t);
void Show() const { q.show(); n.show();}
};
// member definition
template <typename T>
template<typename V>
class beta<T>::hold
{
private:
V val;
public:
hold(V v = 0) : val(v) {}
void show() const { std::cout << val << std::endl; }
V Value() const { return val; }
};
// member definition
template <typename T>
template <typename U>
U beta<T>::blab(U u, T t)
{
return (n.Value() + q.Value()) * u / t;
}
The definitions have to identify T
, V
, and U
as template parameters. Because the templates are nested, you have to use the
template <typename T>
template <typename V>
syntax instead of this syntax:
template<typename T, typename V>
The definitions also must indicate that hold
and blab
are members of the beta<T>
class, and they use the scope-resolution operator to do so.
18.191.189.23