如何判断一个类是否包含子类/类型?
我们能否有一个 SFINAE 技巧来知道该类是否具有某些子类/类型。例如,
template<typename TYPE> // searches for "my_type"
struct has_inner_type {
enum { value = <???> };
};
以下是示例:
struct A {
class my_type {}; // has_inner_type::value = true
};
struct B { }; // has_inner_type::value = false
struct C { typedef int my_type; }; // has_inner_type::value = true
我尝试了一些技巧,但大部分都因预期的编译器错误而失败。 用法:
bool b = has_inner_type<A>::value; // with respect to "my_type"
编辑:我重新编辑了我的问题,因为似乎不可能将 my_type
作为第二个参数传递给 has_inner_type
。因此,到目前为止的问题是仅查找特定类型作为 my_type
。我已经尝试过此代码,但不起作用。
Can we have a SFINAE trick to know, if the class has certain subclass/type. Something like,
template<typename TYPE> // searches for "my_type"
struct has_inner_type {
enum { value = <???> };
};
Following are the examples:
struct A {
class my_type {}; // has_inner_type::value = true
};
struct B { }; // has_inner_type::value = false
struct C { typedef int my_type; }; // has_inner_type::value = true
I tried few tricks, but falling short mostly with expected compiler errors.
Usage:
bool b = has_inner_type<A>::value; // with respect to "my_type"
Edit: I have re-edited my question, as it seems that it's impossible to pass my_type
as second parameter to has_inner_type
. So, as of now the question is to find only a specific type as my_type
. I have tried this code, which doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是我在问题中发布的维基百科链接中的答案! (感谢@nm。)
这是演示。
Following is the answer which was present in the wikipedia link I posted in the question !! (thanks to @n.m.)
Here is the demo.