Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component bslmf_nthparameter
[Package bslmf]

Metafunction to return the Nth type parameter in a parameter pack. More...

Namespaces

namespace  bslmf

Detailed Description

Outline
Purpose:
Metafunction to return the Nth type parameter in a parameter pack
Classes:
See also:
Description:
This component contains a metafunction that treats a parameter pack of types as compile-time array of types, returning the Nth type (counting from zero). It is useful for implementing types like tuple that need access to a specific element of a parameter pack.
Usage:
We wish to implement a tuple-like class that holds a heterogeneous collection of elements, each of which might have a different type. The metafunction, my_tuple_element<I, my_tuple<ELEMS...>>Type would be type of the Ith element in the tuple (where I is zero-based).
First, we define our my_tuple class template. The body of the class is unimportant for this usage examples:
 template <class... ELEMS>
 class my_tuple {
     // ...
 };
Then, we use bslmf::NthParameter to implement my_tuple_element:
 #include <bslmf_nthparameter.h>

 template <std::size_t I, class TUPLE>
 struct my_tuple_element;  // Not defined

 template <std::size_t I, class... ELEMS>
 struct my_tuple_element<I, my_tuple<ELEMS...> > {
     typedef typename bslmf::NthParameter<I, ELEMS...>::Type Type;
 };
Finally, we test this implementation using bsl::is_same:
 #include <bslmf_issame.h>

 int main()
 {
    typedef my_tuple<int, short, char*> ttype;

    assert((bsl::is_same<int,    my_tuple_element<0, ttype>::Type>::value));
    assert((bsl::is_same<short,  my_tuple_element<1, ttype>::Type>::value));
    assert((bsl::is_same<char *, my_tuple_element<2, ttype>::Type>::value));

    assert(! (bsl::is_same<short, my_tuple_element<0, ttype>::Type>::value));
 }