C++11 如何代理只有其名称和父类的类函数
C++11 how to proxy class function having only its name and parent class?
我想知道是否可以使用 boost::mpl/预处理器或一些 noce C++11 功能从类类型和函数名称创建函数代理。
假设我们有:
inline void set_email(const ::std::string& value);
inline void set_email(const char* value);
课堂内电子邮件。我们知道它有set_email函数,我们想创建一个带有 API 的 prox 类,例如
PROXY(Email, set_email, MyEmail)
Email * email = new Email();
MyEmail * myEmail = new MyEmail(email);
并有能力调用set_email过载中的任何一个。是否有可能以及如何创建这样的类来代理不知道类型(仅名称)的任意数量的重载函数?
这个怎么样:
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)...));
}
};
相关文章:
- 在派生类中将公共父析构函数设为私有
- 是否有 lint 工具可以检查子类虚拟函数是否与父类定义匹配?
- 重写子类构造函数中的父类变量
- 如何将派生类作为函数参数传递:如果派生类成员不在父类中怎么办
- 如何从派生类实例调用父类重载函数
- 是否有必要调用父构造函数而不使用子类构造函数的参数?
- C++:使用父类运算符函数更新子类对象的继承变量
- 使用父类构造函数初始化列表中的嵌套类字段中设置值
- 指向父类中派生类的函数的指针
- 通过调用两个参数化的父类构造函数来构造继承的类
- 重写父类的函数
- 从父级调用派生类重写函数.C++
- c++ 派生基类 Friend 函数访问父级上的私有函数
- 带模板的继承(访问子类成员函数内父类的变量和对象)
- 传递对象的地址会导致调用父类构造函数
- 如何从父类调用函数
- 为什么C++不使用父类构造函数?
- C++继承:调用父类构造函数
- 为什么派生类对象在没有参数的情况下自动调用父类构造函数
- 为什么我不能将命名空间放在父类构造函数调用中?