Boost logo

Boost :

Subject: Re: [boost] metaprogramming: associating a type with another type
From: DE (satan66613_at_[hidden])
Date: 2010-01-29 14:28:11


i'm very glad you are asking

on 29.01.2010 at 21:12
 Simonson, Lucanus J wrote :
> This here makes no sense. The property template does not depend on
> t and the typedef is cyclicly defined. I'm guessing you intended:

> template<typename t>
> struct property {
> typedef typeof(get_prop((t*)42) type;
> };
yes you are write here, it's a typo
the source at codepad seems to be typo-free

> But even so I don't quite understand what you're driving at because
> you never use the property template in your example.
i use it to define default template parameter in the 'base'
declaration

> Is the point
> that you never have to specialize traits for the property type of a
> class that derives from base?
yep

> Isn't deriving from base a stronger
> requirement than the specialization of a metafunction?
indeed
this technique is intended for such systems, not for traits driven
ones

> I'm also confused by this:

>> template<typename type, typename p = typename property<type>::type>
>> struct base {
>> friend p get_prop(type*) { return p(); }
>> };

> property<type>::type will always return property0. Is your
> intention that people fully specialize property<> for their type to
> override the default behavior, or to specialize get_prop<>?
ideally user will not ever touch property<> class
here is the interesting part comes into play
when you define a type deriving from base<my_type, propertyX> an
appropriate function is introduced (as a friend), namely
propertyX get_prop(my_typ*) {/*...*/}
but when a type deriving base<my_type> is defined the second template
argument computed automatically as property<type>::type
but since the needed function is not yet introduced it is defined as
the default value (property0 in the example)

> Since
> I'm not sure what your goal is I can't say there is anything wrong
> with your design, but it seems to me that there are elements of your
> design that serve no obvious function. Perhaps you can provide a
> larger example to make the benefit of your approach clearer to those
> of us who haven't finished our morning coffee yet.
my goal is to ease definition of template functions and
metaprogramming
particularly the ability to define expressions (in an expression
templates based lib) in the form

    struct s : public base<s> {};
or
    struct X : public base<s, propertyX> {};

and be able to write template functions which handle either all of the
types derived from base or types associated with a specific property

  template<typename type1, typename type2>
  int foo(const base<type1>&, const base<type2>&)
  {//handles ANY two types derived from base with arbitrary properties
  }

  template<typename type1, typename type2>
  int foo(const base<type1>&, const base<type2, propertyX>&)
  {//the first argument can be ANY type but the second must be associated with propertyX
  }

that is

  s object;
  X x;
  foo(s, s); //compiles
  foo(s, x); //compiles
  foo(x, x); //compiles
  bar(s, x); //compiles
  bar(x, s); //does not compile
  bar(x, x); //does not compile

-- 
Pavel

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk