Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A std::map like container that maps types to values

I'm looking for a hybrid meta-container/container class. I want a class that maps a compile-time type to a runtime value. A code snippit is worth 1024 words so:

struct Foo { /* ... */ };
struct Bar { /* ... */ };

int main()
{
   meta_container<Foo,float,int> mc;
   mc.get<float>() = 1.0f;
   mc.get<Foo>().method(blah);
   mc.get<Bar>(); //compiler error
}

This is really boring stuff. An implementation using variadic templates would be interesting enough but the interface is very simple.

The part that makes this more difficult is this last feature I want.

void foo( const meta_constainer<Foo,Bar,Baz>& mc );

//make_mc is sorta like make_pair or make_tuple.

int main()
{
   foo( make_mc(Foo(), Bar(), Baz()) );  // not really interesting
   foo( make_mc(Bar(), Foo(), Baz()) );  // this is more challenging
   foo( make_mc(Foo()) );  // this might be difficult as well.
}

I could write such a container, but I'd like to find one that's already written/debugged. My biggest stumbling block has been lack of good keywords to search for (heterogeneous container is not what I want).

Is there a Boost library that has this or a similar interface?

What is this thing called, so I can google it more effectively?


update:

I am not looking for:

  • boost::mpl::map
    This maps a compile-time value to a compile-time value
  • std::map<*,boost::any>
    This maps a static type runtime value to a dynamic type runtime value
  • std::map<*,boost::variadic<*>>
    This maps a static typed runtime value to a variable type runtime value
  • std::map<typeid,boost::variadic<*>>
    This is close to what I want except that it uses RTTI and it is not a compile error if accessed with the wrong type.
like image 784
deft_code Avatar asked Oct 20 '10 16:10

deft_code


1 Answers

boost::fusion : http://www.boost.org/doc/libs/1_44_0/libs/fusion/doc/html/index.html

like image 67
Nim Avatar answered Sep 20 '22 06:09

Nim