typedef typename _Mybase::value_type value_type;
typedef
defines a new type for use in your code, like a shorthand.
typename
letting the compiler know that value_typ
is a type and not a static member of _MyBase
.
# include <iostream>
# include <typeinfo>
namespace ft
{
template <typename T>
/*
T is the type of the elements that the container holds,
----------
typename or class are both acceptable as the keyword to declare a type parameter.
*/
class AB
{
private:
typedef T value_type1;
value_type1 a;
public:
typedef T value_type2;
AB(value_type2 a) : a(a) {}
void print() { std::cout << a << std::endl; }
void print_type() { std::cout << typeid(a).name() << std::endl; }
};
template <class T1>
class CD
{
private:
typedef T1 value_type1;
value_type1 a;
public:
typedef T1 value_type2;
CD(value_type2 a) : a(a) {}
void print() { std::cout << a << std::endl; }
void print_type() { std::cout << typeid(a).name() << std::endl; }
};
} // namespace ft
int main()
{
ft::AB<int> ab(42);
ft::AB<std::string> ab2("Hello");
ft::CD<int> cd(42);
ft::CD<std::string> cd2("Hello");
ab.print();
ab.print_type();
ab2.print();
ab2.print_type();
cd.print();
cd.print_type();
cd2.print();
cd2.print_type();
return 0;
}
Ok, let us explain The code above
typename
I use only with template similar to class
, actually typename
and class its same.