I'm trying to learn a little about metaprogramming patterns and I'm currently playing with variable patterns.
In his presentation, “Variadic Templates - Fandaki”, Alexandrescu presents an implementation of a small tuple, which I am trying to build and possibly expand a bit. (I know this is a toy example, I'm just trying to learn a little bit more about C ++). However, I have a little problem with its code.
There he is:
template <typename... Ts> class tuple {}; template<size_t, typename> struct tuple_element; template<typename T, typename... Ts> struct tuple_element<0, tuple<T, Ts...>> { typedef T type; }; template <size_t k, typename T, typename... Ts> struct tuple_element<k, tuple<T, Ts...>> { typedef typename tuple_element<k-1,tuple<Ts...>>::type type; }; template<size_t k, typename... Ts> typename std::enable_if<k == 0, typename tuple_element<0,tuple<Ts...>>::type&>::type get(tuple<Ts...>& t) {return t.head_;} template<size_t k, typename T, typename... Ts> typename std::enable_if<k != 0, typename tuple_element<k,tuple<T,Ts...>>::type&>::type get(tuple<T,Ts...>& t) { tuple<Ts...> & super = t; return get<k-1>(super); } template <typename T, typename... Ts> class tuple<T,Ts...> : private tuple<Ts...> { private: T head_; }; int main(int argc, char *argv[]) { tuple<int,std::string> t; get<0>(t) = 10; get<1>(t) = std::string("test"); std::cout<<get<0>(t)<<std::endl; }
In order to work correctly, the get function must be (this is also mentioned on this slide, see 32). But what does a friend’s announcement look like? I tried different approaches but could not get it to work. When I change the code from private to public inheritance and change the access rules for head_ for the public, it works.
thanks for the help
Kevin