检查模板参数是否有成员函数
可能的重复:
是否可以编写检查函数是否存在的 C++ 模板?
这与我的 之前的问题。我想检查模板参数是否包含成员函数。
我尝试了与上一个问题中接受的答案类似的代码。
struct A
{
int member_func();
};
struct B
{
};
template<typename T>
struct has_member_func
{
template<typename C> static char func(???); //what should I put in place of '???'
template<typename C> static int func(...);
enum{val = sizeof(func<T>(0)) == 1};
};
int main()
{
std::cout<< has_member_func<B>::val; //should output 0
std::cout<< has_member_func<A>::val; //should output 1
}
但我不知道应该用什么来代替 ???
才能使其正常工作。 我对 SFINAE 概念很陌生。
Possible Duplicate:
Is it possible to write a C++ template to check for a function's existence?
This is very similar to my earlier question. I want to check whether a template argument contains a member function or not.
I tried this code similar to that in the accepted answer in my previous question.
struct A
{
int member_func();
};
struct B
{
};
template<typename T>
struct has_member_func
{
template<typename C> static char func(???); //what should I put in place of '???'
template<typename C> static int func(...);
enum{val = sizeof(func<T>(0)) == 1};
};
int main()
{
std::cout<< has_member_func<B>::val; //should output 0
std::cout<< has_member_func<A>::val; //should output 1
}
But I have no idea what should I put in place of ???
to make it work.
I am new to SFINAE concept.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对 MSalters 的想法进行了一些修改 是否可以编写一个 C++ 模板来检查函数是否存在?:
Little modification of MSalters' idea from Is it possible to write a C++ template to check for a functions existence? :