From: Sebastian Redl (sebastian.redl_at_[hidden])
Date: 2006-02-05 15:00:16


Edward Diener wrote:

>Can you give an example of the compile-time use of reflection for
>templates and how this would be done ? Or have I completely
>misunderstood what you and Sebastian Redl are discussing ?
>
>
Although this came up in a different branch of discussion, the topic of
David and my discussion was about the hardships of implementing a
solution that could, at run-time, instantiate a template and load the
newly generated code into the application.

But for an example of compile-time reflection, I posted something a few
days ago, in the thread about a policy selector using a mpl vector of
policy classes. There, I used the mpl utility macro
BOOST_HAS_NAMED_TYPEDEF (I think is the name of the macro.) This is one
very limited example of compile-time reflection. Real language support
would allow for far more interesting things here.
My example implemented this compile-time pseudo-code:

metafunction type policy_type(type policy)
{
  if(policy.has_typedef("policy_type")) {
    return policy.policy_type;
  } else {
    return not_a_policy;
  }
}

Overloads (= specializations) could be provided for types that had a
policy_type but no nested typedef policy_type.
The has_typedef would basically be a part of compile-time reflection.

However, reflection would be far more powerful. Instead of just testing
for the existence, you could for example enumerate all typedefs.

Sebastian Redl