C ++ 11 how does a proxy class function only have a name and a parent class?

I wonder if it is possible to use boost :: mpl / preprocessor or some noce C ++ 11 functions to create a proxy for a function from the class type and function name.

Say we had:

inline void set_email(const ::std::string& value); inline void set_email(const char* value); 

Inside the Email class. We know that there is a set_email n function, we want to create a prox class with an API like

 PROXY(Email, set_email, MyEmail) Email * email = new Email(); MyEmail * myEmail = new MyEmail(email); 

and have the ability to call any of the set_email overloads. Is it possible and how to create a class that proxies any number of overload functions without knowing their types (only names)?

+3
c ++ boost c ++ 11 boost-preprocessor boost-mpl
source share
1 answer

How about this:

proxy_macro.hpp

 #include <type_traits> #include <utility> #define PROXY(proxified, member_function, proxy_name) \ class proxy_name \ { \ private: \ proxified & ref_; \ \ public: \ proxy_name(proxified &ref) \ : ref_(ref) \ { \ } \ \ /* general version */ \ template<typename ... Args> \ auto member_function(Args&& ... args) \ -> typename std::enable_if<!std::is_void<decltype(ref_.member_function(std::forward<Args>(args)...))>::value, \ decltype(ref_.member_function(std::forward<Args>(args)...))>::type \ { \ return (ref_.member_function(std::forward<Args>(args)...)); \ } \ \ /* void return type version */ \ template<typename ... Args> \ auto member_function(Args&& ... args) \ -> typename std::enable_if<std::is_void<decltype(ref_.member_function(std::forward<Args>(args)...))>::value, \ void>::type \ { \ ref_.member_function(std::forward<Args>(args)...); \ } \ \ }; 

This compiles and works fine for me on g++ 4.7 :

 #include "proxy_macro.hpp" #include <iostream> #include <string> class Email { public: void set_email(const ::std::string& value) { std::cout << value << std::endl; } void set_email(const char* value) { std::cout << value << std::endl; } int set_email() { return (42); } }; PROXY(Email, set_email, MyEmail) int main(void) { Email mail; MyEmail my_mail(mail); std::string str = "test string"; const char * ptr = "test char pointer"; my_mail.set_email(str); my_mail.set_email(ptr); std::cout << "test return: " << my_mail.set_email() << std::endl; return (0); } 

Edit (smaller version thanks to comments)

proxy_macro.hpp

 #include <type_traits> #include <utility> #define PROXY(proxified, member_function, proxy_name) \ class proxy_name \ { \ private: \ proxified & ref_; \ \ public: \ proxy_name(proxified &ref) \ : ref_(ref) \ { \ } \ \ template<typename ... Args> \ auto member_function(Args&& ... args) \ -> decltype(ref_.member_function(std::forward<Args>(args)...)) \ { \ return (ref_.member_function(std::forward<Args>(args)...)); \ } \ }; 
+4
source share

All Articles