Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return type is a variable in a class

Tags:

c++

I am looking the C++ reference, and I see the

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 the return type, what does the typename tuple_element< I, tuple<Types...> >::type const& means?

My interpertation is that it return a const reference to a general type of tuple_element::type , but I think that the tuple_element::type is like below

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

but why it can be used as a type? I cannot understand it.

like image 831
Jason Avatar asked Jul 24 '13 06:07

Jason


3 Answers

The type in typename tuple_element< I, tuple<Types...> >::type is not a variable. It is a type within another type (tuple_element< I, tuple<Types...> >).

Referencing a type within another type can be done by using ::, the scope resolution operator, just as you do when referencing a variable or function within a class or a 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;
}
like image 131
Mark Garcia Avatar answered Oct 13 '22 00:10

Mark Garcia


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

struct myClass
{
    typedef int myIntType;
};

you can write:

myClass::myIntType i = 3;
like image 25
neuro Avatar answered Oct 12 '22 22:10

neuro


From tuple_element reference:

Member types:

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;
};
like image 38
awesoon Avatar answered Oct 12 '22 22:10

awesoon