Boost logo

Boost Users :

From: Noah Roberts (roberts.noah_at_[hidden])
Date: 2008-05-20 12:43:28


Hansi wrote:
> Hello,
>
> at the moment I want to make a getter function for a tuple type. The
> tuple is internally hidden in a class. For that I want to make a
> function which returns the values inside the tuple. The best solution
> would be if I can make a enum which defines the position inside the
> tuple and a template function which returns the value for this tuple.
>
> I have tested the following, but it doesn't work:
>
> typedef struct Members
> {
> enum Member
> {
> name = 0,
> value = 1,
> };
> }Members;
>
> template<enum Member member>
> element<0, Properties::Property>::type name()(const
> boost::tuples::tuple<std::wstring, boost::any>& prop)
> {
> return boost::tuples::get<member>(prop);
> }

I didn't quite understand your goals here so I implemented both I
thought you could mean:

#include <boost/tuple/tuple.hpp>
#include <string>
#include <iostream>

template < typename T1, typename T2 >
struct Test
{
   typedef boost::tuple<T1,T2> tuple_t;
   tuple_t tup;

   enum Members { NAME, VALUE };

   typename boost::tuples::element<NAME, tuple_t>::type name() { return
tup.get<NAME>(); }

   template < Members mem >
   typename boost::tuples::element<mem, tuple_t>::type member() { return
tup.get<mem>(); }
};

int main()
{
   Test<std::string, int> t;
   t.tup = Test<std::string,int>::tuple_t("Test", 42);

   std::cout << t.name() << std::endl;
   std::cout << t.member<Test<std::string,int>::VALUE>() << std::endl;

   std::cin.get();
}

Get the metaprogramming book by Abrahams and Gurtovoy. It's completely
necessary to understand metaprogramming to be able to use these aspects
of boost.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net