views:

219

answers:

1

I'm attempting to write a policy-based host class (i.e., a class that inherits from its template class), with a twist, where the policy class is also templated by the host class, so that it can access its types. One example where this might be useful is where a policy (used like a mixin, really), augments the host class with a polymorphic clone() method. Here's a minimal example of what I'm trying to do:

template <template <class> class P>
struct Host : public P<Host<P> > {
  typedef P<Host<P> > Base;
  typedef Host* HostPtr;
  Host(const Base& p) : Base(p) {}
};

template <class H>
struct Policy {
  typedef typename H::HostPtr Hptr;
  Hptr clone() const {
    return Hptr(new H((Hptr)this));
  }
};

Policy<Host<Policy> > p;
Host<Policy> h(p);

int main() {
  return 0;
}

This, unfortunately, fails to compile, in what seems to me like circular type dependency:

try.cpp: In instantiation of ‘Host<Policy>’:
try.cpp:10:   instantiated from ‘Policy<Host<Policy> >’
try.cpp:16:   instantiated from here
try.cpp:2: error: invalid use of incomplete type ‘struct Policy<Host<Policy> >’
try.cpp:9: error: declaration of ‘struct Policy<Host<Policy> >’
try.cpp: In constructor ‘Host<P>::Host(const P<Host<P> >&) [with P = Policy]’:
try.cpp:17:   instantiated from here
try.cpp:5: error: type ‘Policy<Host<Policy> >’ is not a direct base of ‘Host<Policy>’

If anyone can spot an obvious mistake, or has successfuly mixing CRTP in policies, I would appreciate any help.

+1  A: 

In fact the problem is due to HostPtr declaration not having seen yet when you inherit from the policy. There is some discussion about the exact semantics where these declarations are visible by instantiated templates, which has pretty complex issues, see this defect report.

But in your case, the situation is clear: Before the class body, no code can see any declaration of class members, and so your code fails. You could pass the type as a template argument

template <template <class,class> class P>
struct Host : public P<Host<P>, Host<P>* > {
  typedef P<Host<P> > Base;
  Host(const Base& p) : Base(p) {}
};

template <class H, class Hptr>
struct Policy {
  typedef Hptr HostPtr;
  HostPtr clone() const {
    return Hptr(new H((Hptr)this));
  }
};

If there are more types, you may decide to pass a trait

template <class Host>
struct HTraits {
  typedef Host *HostPtr;
  // ...
};

template <template <class,class> class P>
struct Host : public P<Host<P>, HTraits< Host<P> > > {
  typedef P<Host<P> > Base;
  Host(const Base& p) : Base(p) {}
};

template <class H, class Htraits>
struct Policy {
  typedef typename Htraits::HostPtr HostPtr;
  HostPtr clone() const {
    return Hptr(new H((Hptr)this));
  }
};
Johannes Schaub - litb