检测类型是否存在
SFINAE 允许我们检测类型是否具有某些数据成员或成员函数。它也可以用来检测某种类型是否存在吗?背景:我想知道是否包含
。
SFINAE allows us to detect if a type has certain data members or member functions. Can it also be used to detect if a type exists at all? Background: I want to know whether <vector>
was included or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它可以做到,尽管我只设法让它在 C++0x 中工作。
该结构非常奇怪,尽管应该有某些明显的简化,但它们不起作用。更重要的是,为什么不直接
#include
来确定呢?It can do, although I've only managed to make it work in C++0x.
The structure is very curious and although there should be certain obvious simplifications, they don't work. More importantly, why don't you just
#include <vector>
to be certain?我对这个问题有一个问题:
如果未包含
,那么如何在不知道的情况下测试翻译单元中是否引入了某种类型?您实际上是在要求编译器测试
vector
是否存在,但是如果不先声明vector
,您怎么能问这个问题呢?我不知道任何解决方案如何工作并符合标准,但我不是标准主义者,所以可能有一个我不知道的警告。
I have one problem with the question:
If
<vector>
was not included, then how can you test whether a type has been introduced in the translation unit without knowing about it ?You are effectively asking the compiler to test if
vector
exists, but how can you ask that without declaringvector
first and foremost ?I don't see how any solution could possibly work and be standard compliant, but I am no standardista so there might be a caveat I am not aware of.