Consider
I'd like for the user to be able to specify the desired policy during template instantiation. For instance:Code:# include <iostream> template < typename value_type > struct your_policy { static value_type write_to_register ( value_type value ) { return ( 1 ); } static value_type read_from_register ( value_type value ) { return ( 2 ); } }; // your_policy template < typename value_type > struct our_policy { static value_type write_to_register ( value_type value ) { return ( 3 ); } static value_type read_from_register ( value_type value ) { return ( 4 ); } }; // our_policy template < typename unsigned_type, typename policy > struct lets_see { public: // only for testing lets_see () {} static unsigned_type & dummy ( void ) { static unsigned_type x; return ( x ); } public: lets_see & operator= ( unsigned_type const val ) { dummy() = policy< unsigned_type >::write_to_register( val ); return ( *this ); } operator unsigned_type ( void ) const { unsigned_type result = policy< unsigned_type >::read_from_register( dummy() ) ; return ( result) ; } };
I'm getting errors though that I'm unsure how to resolve. How would I achieve this?Code:int main ( void ) { lets_see < unsigned int, your_policy < unsigned int > > a; lets_see < unsigned int, our_policy < unsigned int > > b; a.dummy() = 5; std::cout << a << '\n'; std::cin.get(); }




Reply With Quote