これはどう:
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)...); \
} \
\
};
これはコンパイルされ、私にとってはうまくいきます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);
}
編集(コメントのおかげで小さいバージョン)
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)...)); \
} \
};