samedi 28 novembre 2020

Template Aliases - question (tour of c++)

I was going through the template Aliases from Tour of C++. I couldn't understand the below code and how to use it?

template<typename T>
class Vector {
public:
   using value_type = T; 
}

Here he is using the value_type as type alias for typename 'T', Why can't we just use the typename T, since we can pass any type to template(i.e. class Vector). What is the need to have alias for template?

template<typename C>
using Value_type = typename C::value_type;

Here how is value_type in the scope of C, i.e. how can we reference value_type with type 'C', since it is inside class "vector"? does 'value_type' here mean its a Vector? and 'Value_type' mean int::Vector or string::Vector etc..?

template<typename Container>
void algo(Container &c)
{
  Vector<Value_type<Container>> vec;
}

How are these three pieces linked?

Aucun commentaire:

Enregistrer un commentaire