robber.hpp (958B)
1 #ifndef ROBBER_HPP_INCLUDED 2 #define ROBBER_HPP_INCLUDED 3 4 /** 5 * @brief Accessor to non-public member. 6 * 7 * In order to access a non-public member of a class, you have to specialize this template with a tag (see @ref Tag_base) and the member to access. 8 * This trick is from Johannes "litb" Schaub. 9 * 10 * @tparam Tag The tag of the class to rob. 11 * @tparam M The address of the member to access. 12 * 13 * @bug When called on virtual function, only the final override can be accessed. 14 */ 15 template<typename Tag, typename Tag::type M> 16 struct Rob{ 17 friend typename Tag::type get(Tag){ 18 return M; 19 } 20 }; 21 22 /** 23 * @brief Class template to simplify the definition of robber tags. 24 * 25 * This templated can be used alongside the @ref Rob class template with a CRTP. 26 * 27 * @tparam Tag The tag itself. 28 * @tparam Member The type of the member to access. 29 */ 30 template<typename Tag, typename Member> 31 struct Tag_base{ 32 typedef Member type; 33 friend type get(Tag); 34 }; 35 36 #endif 37