Re: [Boost-users] multi-inheritance from elements of mpl::vector? ==> inherit_scattered ( for Barton & Nackmann trick)
no reply - so I found a solution by myself (used for the Barton &
Nackmann trick)!
cu,
Oliver
Code:
#include <iostream>
#include <cstdlib>
#include <string>
#include
struct scattered; template< typename B, typename E
struct scattered : public mpl::deref< B >::type, public scattered< typename mpl::next< B >::type, E > {}; template< typename E > struct scattered< E, E > {}; //////////////////////////////////// template< typename Sequence > struct inherit_scattered : public scattered< typename mpl::begin< Sequence >::type, typename mpl::end< Sequence >::type > {}; //////////////////////////////////// template< typename D > struct F { std::string f() { return static_cast< D & >( * this).str_; } }; template< typename D > struct G { std::string g() { return static_cast< D & >( * this).str_; } }; //////////////////////////////////// class A : public inherit_scattered< mpl::vector< F< A >, G< A > > > { private: template< typename X > friend struct F; template< typename X > friend struct G; std::string str_; public: A( std::string const& str) : str_( str) {} }; int main( int argc, char *argv[]) { A a("Hello World!"); std::cout << a.f() << std::endl; std::cout << a.g() << std::endl; return EXIT_SUCCESS; }
Hi, how is it possible to inherit from all items inside mpl::vector?
template< typename V > class multi : .... // ????? {};
typedef multi < mpl::vector< A ,B, C > > multi _t; multi_t m; // m has all public and protected member functions of A, B and C
Best Regards, Oliver
no reply - so I found a solution by myself (used for the Barton & Nackmann trick)! template< typename D > struct F { std::string f() { return static_cast< D & >( * this).str_; } };
template< typename D > struct G { std::string g() { return static_cast< D & >( * this).str_; } };
That's not the B&N trick; it's simple CRTP. The B&N trick is the one where you stick a friend function that applies to the derived class in the base class. See the operators library for reference. -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (2)
-
David Abrahams
-
Oliver.Kowalke@infineon.com