Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2002-02-02 14:12:40


Today I found this (somewhat limited) solution for cases where you need to
unwind pointer-, reference-, and cv-qualifications at runtime with certain
compilers that don't support partial ordering ;-)

unwind_ptr<Generator>(p), where p is T[const|volatile|const volatile]* will
be the result of Generator::execute((T*)p).

Obviously this could be generalized further:

  template <class Generator, class U>
  inline typename Generator::result_type
  unwind_ptr_cv(U* p, cv_unqualified, Generator* = 0)
  {
      return Generator::execute(p);
  }

  template <class Generator, class U>
  inline typename Generator::result_type
  unwind_ptr_cv(U const* p, const_, Generator* = 0)
  {
      return unwind_ptr(const_cast<U*>(p), (Generator*)0);
  }

  template <class Generator, class U>
  inline typename Generator::result_type
  unwind_ptr_cv(U volatile* p, volatile_, Generator* = 0)
  {
      return unwind_ptr(const_cast<U*>(p), (Generator*)0);
  }

  template <class Generator, class U>
  inline typename Generator::result_type
  unwind_ptr_cv(U const volatile* p, const_volatile_, Generator* = 0)
  {
      return unwind_ptr(const_cast<U*>(p), (Generator*)0);
  }

  template <class Generator, class U>
  inline typename Generator::result_type
  unwind_ptr(U* adopt_me, Generator* = 0)
  {
      typedef typename detail::cv_category<U>::type tag;
      return unwind_ptr_cv<Generator>(adopt_me, tag());
  }

I leave the implementation of cv_category<> as an exercise ;-)

-Dave

===================================================
  David Abrahams, C++ library designer for hire
 resume: http://users.rcn.com/abrahams/resume.html

        C++ Booster (http://www.boost.org)
          email: david.abrahams_at_[hidden]
===================================================


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