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.
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;
}
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;
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; };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With