How do you understand dependent names in C++
I come acr开发者_StackOverflowoss this term "dependent names" typically in the context of templates. However, I rarely touch the latter. Thus naturally would like to know more about the concept of dependent names.
How do you understand it in the context of templates and outside of them? example are critically encouraged!
Dependent names are characterized by a dependency on a template argument. Trivial example:
#include <vector>
void NonDependent()
{
//You can access the member size_type directly.
//This is precisely specified as a vector of ints.
typedef std::vector<int> IntVector;
IntVector::size_type i;
/* ... */
}
template <class T>
void Dependent()
{
//Now the vector depends on the type T.
//Need to use typename to access a dependent name.
typedef std::vector<T> SomeVector;
typename SomeVector::size_type i;
/* ... */
}
int main()
{
NonDependent();
Dependent<int>();
return 0;
}
EDIT: As I mentioned in the comment below, this is an example of a peculiar situation regarding the use of dependent names which appears quite frequently. Sometimes the rules governing the use of dependent names are not what one might instinctively expect.
For instance, if you have a dependent class which derives from a depenent base, but within an scope in which a name from the base class apparently doesn't depent on the template, you might get a compiler error just like below.
#include <iostream>
template <class T>
class Dependent
{
protected:
T data;
};
template <class T>
class OtherDependent : public Dependent<T>
{
public:
void printT()const
{
std::cout << "T: " << data << std::endl; //ERROR
}
};
int main()
{
OtherDependent<int> o;
o.printT();
return 0;
}
This error happens because the compiler will not lookup name data
inside the base class template since it doesn't dependent on T
and, consequently, it is not a depedent name. The ways to fix are using this
or explicitly telling the dependent base class template:
std::cout << "T: " << this->data << std::endl; //Ok now.
std::cout << "T: " << Dependent<T>::data << std::endl; //Ok now.
or placing using
declaration:
template <class T>
class OtherDependent : public Dependent<T>
{
using Dependent<T>::data; //Ok now.
....
};
A dependent name is essentially a name that depends on a template argument.
When using templates there is a distinction between the point of definition of the template and the point of instantiation i.e. where you actually use the template. Names that depend on a template don't get bound until the point of instantiation whereas names that don't get bound at the point of definition.
A simple example would be:
template< class T > int addInt( T x )
{
return i + x.toInt();
}
where a declaration or definition of i
would need to appear before the definition given above since i
does not depend on the template argument T
and is therefore bound at the point of definition. The definition of the toInt
member of the as-yet-unknown-type x
variable only has to appear before the addInt
function is actually used somewhere as it is a dependent name ( technically the point of instantiation is taken as the nearest enclosing global or namespace scope just before the point of use and so it has to be available before that ).
精彩评论