这是一个理论问题:是否可以在其定义中获取未命名结构的类型。
int main() {
struct {
using ThisType = ???? //what do i put here?
} _; // the instance
}
我尝试从成员函数指针中提取类,但没有成功
template<typename>
struct get_class;
template<typename Class>
struct get_class<void(Class::*)()>{using type = Class;};
int main(){
struct {
void getThis(){}
using ThisType = typename get_class<decltype(getThis)>::type; // error cant know which function to call to determine
// i tried to static_cast but I need to know the class type still.
} _;
}
这可能吗?
这似乎可以解决问题(使用辅助函数 f):
template <typename fn_t>
struct deduce_class_t
{
};
template <typename retval_t, typename class_t, typename... args_t>
struct deduce_class_t <retval_t(class_t::*)(args_t...)>
{
using type_t = class_t;
};
struct
{
void f();
using type_t = typename deduce_class_t<decltype(&f)>::type_t;
} _;