What are dependent scope type in templates?

Share the Article

Dependent type are data types which depend on the template parameter. These dependent types are accessed via scope resolution operator.

template<typename T> class MFPointer { public: typedef T* ptype; };

In above example, the member “ptype” is dependent scope datatype. This is defined inside a Template class MFPointer<T> and actual datatype (T*) depends on template parameter T. For instance, for MFPointer<int>, the ptype shall become int* and for MFPointer<float>, the ptype will be float*.

To use or access the ptype, the program shall use an expression with scope resolution operator, like, “MFPointer<int>::ptype”

Variable Declaration using Dependent type:

In the above example, ptype is actually a data type which was defined by typedef. We may use this datatype to write any declaration of any variable. Example,

typename MFPointer<T>::type m;

The declaration of “m” makes sense only if Pointer<T>::ptype is a datatype, however, there is no guarantee for this. This is because Pointer<T>::ptype may also be a static data member or enum.

To ensure C++ compiler that program knows that this is a datatype and not anything else (a static variable or enum variable or member variable), the keyword typename must precede the declaration . This keyword gives guarantee that iterator is a type.


#include <iostream> //main header using namespace std; //namespace template<typename T> class MFPointer { public: typedef T* ptype; }; template<typename T> void funda(T tx) { MFPointer<T>::ptype m; //Wrong declaration //typename MFPointer<T>::ptype m; //Right declaration } int main() { int ii; funda(ii); //Instantiation as funda<int> return 0; }

In this example,

  1. funda(ii) takes a argument of type integer. Therefore, the parameter T of template “funda<T>” will resolve to become integer (funda<int>).
  2. This “funda” template function internally uses a dependent scope type to declare a variable. The “MFPointer<T>::ptype”, therefore, becomes “MFPointer<int>::ptype”.
  3. Ultimately, “MFPointer<int>::ptype” becomes “int*”

The compiler will generate error:

need typename for using dependent types related to template class

Negative Scenario : ptype is not a type

Following example shows ptype is static variable for MFPointer<int> specialization. Such design may lead of potential issues, therefore, usage of “typename” help in identifying the issues during debugging.

template<typename T> class MFPointer { public: typedef T* ptype; }; template<> class MFPointer<int> { public: static int ptype = 0; };

Any usage of dependent scope type with typename keyword shall make the code readable. The user shall understand that they are referring to a type or non-type. The declaration cannot work for MFPointer<int> as ptype is non-type.

typename MFPointer<int>::type m; //Wrong typename MFPointer<char>::type n; //Right

Main Funda: typename keyword tells compiler that the dependent scope item is a type

Related Topics:

What are dependent scope type in templates?
What is an implicit interface? 
Calling member functions in template base class
What is template meta programming (TMP)
How delete keyword can be used to filter polymorphism
Concept of Inline Functions
What is reference collapsing?
How std::forward( ) works?
How std::move() function works?
Smart Pointers: shared_ptr <T> 
Smart Pointers: unique_ptr<T>
What is move constructor ?
Understanding Constant Variables

Share the Article

Leave a Reply

Your email address will not be published. Required fields are marked *