我有一个班级:
class my_happy_class
{
// Something here!
};
现在,我正在构建一个可以在类中添加的宏,根据类的某些属性,可以添加某个类型的成员。这些属性(例如,具有给定签名的方法的存在)都可以通过模板和SFINAE轻松确定。
class my_happy_class
{
my_happy_macro(my_member); // Adds my_member of a type that is determined from some class properties
};
现在,为了确定类属性,我需要来命名它。显然,我可以扩展my_happy_macro
以接受类的名称作为第一个参数:
class my_happy_class
{
my_happy_macro(my_happy_class, my_member); // Adds my_member of a type that is determined from some class properties
};
但那感到多余和丑陋。所以我想知道:有没有办法确定我自主上课的类别?
class my_happy_class
{
typedef /*(Some super complex expression here that never explicitly says
"my_happy_class", but can use templates, external helper classes,
anything)*/ myself;
// Use myself as an alias for my_happy_class, do the tests, add the member.
};
我尝试使用成员指针:
template <typename type> type parent(void (type :: *) ());
class my_happy_class
{
public:
void f();
typedef decltype(parent(&f)) myself;
};
但这不起作用,因为我必须在调用&my_happy_class::f
时明确写parent
。所以现在我缺乏想法。我可以使用任何解决方案吗?