检查 c++ 中是否存在具有给定签名的函数;
所以我正在寻找方法来检查具有特定参数的函数是否存在。我有一个模板方法,它依赖于外部函数(来自类的外部)来完成这项工作:
template <class Moo>
void exportDataTo(Moo& ret){
extended_solid_loader(ret, *this);
}
在项目中的多个点上,我有为不同类型定义extend_solid_loader的宏,但现在我希望能够使用默认函数如果尚未为该特定类类型定义extended_solid_loader。
我遇到过这个: 是否可以编写检查函数是否存在的模板? 但它似乎有点不同,因为我不是检查方法,而是检查具有特定参数类型的函数的定义。
现在这可能吗?
So I was looking for ways to check whether a function with a particular argument exists. I have a templated method which relies on an external function (external from the class) to do the job:
template <class Moo>
void exportDataTo(Moo& ret){
extended_solid_loader(ret, *this);
}
At multiple points in the project I have macros which define extended_solid_loader for different types, but now I want to be able to use a default function if extended_solid_loader hasn't been defined for that particular class type.
I came across this:
Is it possible to write a template to check for a function's existence?
but it seems a little different, in that I'm not checking for a method, but rather a definition of a function with a particular argument type.
Is this possible right now?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需为
extended_solid_loader
提供一个函数模板即可提供默认实现,而想要使用默认实现以外的其他内容的用户只需对其进行专门化即可。You can just provide a function template for
extended_solid_loader
providing a default implementation, and users who want to use something other than the default implementation just specialize that.您实际上不必做任何特别的事情。只需确保该函数有一个可供模板使用的版本,然后让 ADL 来完成这些肮脏的工作即可。看看这个例子:
也可以在没有命名空间的情况下工作(非模板有优先权)。我只是用它来表明当你这样做时 ADL 会捡起它。
你原来的问题很有趣。在 C++0x 中找到了一种方法:
You don't actually have to do anything particularly special. Just make sure there's a version of that function available to the template and let ADL do the dirty work. Check out this example:
Works without the namespace stuff too (non-templates have preference). I just used that to show that ADL will pick it up when you do.
Your original question was interesting. Found a way to do it in C++0x: