Boost logo

Ublas :

From: Ian McCulloch (ianmcc_at_[hidden])
Date: 2005-02-06 10:24:17


Michael Stevens wrote:

[...]

> In fact simply instantiating the vector_range template with the following
> fails:
> myproject(a+b, ublas::range(0,0))
>
> The problem is occurring because the type
> vector_range<V>::vector_temporary_type is not defined in such an
> instantiation (where V is a vector_binary). Why this is an error I am not
> sure as the type is never used and class member functions which depend on
> it are ever instantiated. This seems to go against the usual "is not an
> error" rules related to template definitions.

If a template declares a type, then that type must exist. It doesn't have
to be a complete type, and if the type is itself a template then simply
naming it doesn't cause it to be instantiated, but it must have been
previously declared.

template <typename T>
struct foo
{
   typedef this_type_does_not_exist type;
};

This is an error at the point of declaration (ie, even without instantiating
any foo classes), unless there is a previous definition of type
'this_type_does_not_exist'.

template <typename T>
struct bar
{
   typedef typename T::this_type_does_not_exist type;
};

No error here in the declaration, but at the point of instantiating bar<X>,
X::this_type_does_not_exist must have been previously declared. Note that
this declaration would also cause the instantiation of X itself.

The rule of thumb is, whenever a template is instantiated all of the
declarations must be legal - so all the typedefs must be previously
declared types. That does not cause member functions to be instantiated
though. Simply naming a templated type does not cause it to be
instantiated either. eg, it would be fine to declare a template function
that returns bar<void>. It would be an error to instantiate it though,
because that would require instantiating the bar<void> copy-ctor and
bar<void> is erroneous. It would be OK if it was a pointer or reference to
bar<void> though, I think, similarly to the case of pointers to incomplete
types.

Cheers,
Ian