Return type - a variable in the class

I am looking for a C ++ link and I see

template <size_t I, class... Types> typename tuple_element< I, tuple<Types...> >::type const& get(const tuple<Types...>& tpl) noexcept; 

and what I cannot understand is the return type, which means typename tuple_element< I, tuple<Types...> >::type const& ?

My interaction is that it returns a const reference to the generic type tuple_element::type , but I think tuple_element::type looks below

 Class A{ public: int B; } A::B = .........; 

but why can it be used as a type? I can’t understand this.

+6
source share
3 answers

type in typename tuple_element< I, tuple<Types...> >::type not a variable. This is a type inside another type ( tuple_element< I, tuple<Types...> > ).

A reference to a type inside another type can be done using :: , the scope resolution operator, just as you do by referring to a variable or function inside a class or namespace.

Example:

 namespace my_namespace { struct my_type { typedef int some_type; // some_type here is an alias for int (both are types) }; } int main() { my_namespace::my_type::some_type some_variable; } 
+7
source

Here, your class member is not a variable, but a type defined in the scope of the class. If you need a simple example:

 struct myClass { typedef int myIntType; }; 

You can write:

 myClass::myIntType i = 3; 
+4
source

From tuple_element reference :

Types of participants:

 type: the type of Ith element of the tuple, where I is in [0, sizeof...(Types)) 

Possible implementation:

 template< std::size_t I, class T > struct tuple_element; // recursive case template< std::size_t I, class Head, class... Tail > struct tuple_element<I, std::tuple<Head, Tail...>> : std::tuple_element<I-1, std::tuple<Tail...>> { }; // base case template< class Head, class... Tail > struct tuple_element<0, std::tuple<Head, Tail...>> { typedef Head type; }; 
+2
source

Source: https://habr.com/ru/post/950154/


All Articles