
Zitat von Sohail Somani <sohail@taggedtype.net>:
On 10-06-28 8:00 AM, Stefan Strasser wrote:
template<class Mapping> friend void introspect(Mapping mapping,type<A>){ mapping (base_class<A_base>() ) (member<A,int ,&A::m_a>() ) (member<A,float ,&A::m_b>() ) (member<A,std::vector<int>,&A::m_vec>()) (); }
I think a library like this would be useful.
Isn't it simpler to specialize a template? Why does this need to be done at run-time?
I assume you mean as opposed to something like typedef mpl::vector<member<...>,member<...> > introspect; there are several reasons for this: - I prefer the syntax above - although it is a runtime function it isn't really executed at runtime. an optimizing compiler generates no code. - if you need a MPL sequence, you can easily obtain one: struct functor{ template<class Sequence> void operator()(Sequence) const{ } }; intro::apply_local_sequence<A>(functor()); - you can associate state with a member. for example: mapping (member<...>(),"member_name") (); which is useful e.g. for algorithms serializing to XML, or mapping a member to an SQL database field. - most algorithms only need to iterate members and don't need a MPL sequence, which saves lots of instantiations. they simply implement a functor that gets called for each member and call intro::for_each<A>(functor());
How do you introspect a type's constructors?
none of the algorithms need reflection of constructors or other member functions. if you need it it can be added without changing the library. it would probably look something like: template<class A0,class A1...> struct constructor{}; mapping (constructor<arg_type1,arg_type2>()) ();