这个Q是以下内容的扩展:
模板化检查类成员函数是否存在?
This Q is an extension of:
Templated check for the existence of a class member function?
是否有任何实用程序可以帮助您找到:
Is there any utility which will help to find:
#define HasMember(NAME)
template<class Class, typename Type = void>
struct HasMember_##NAME
{
typedef char (&yes)[2];
template<unsigned long> struct exists;
template<typename V> static yes Check (exists<sizeof(static_cast<Type>(&V::NAME))>*);
template<typename> static char Check (...);
static const bool value = (sizeof(Check<Class>(0)) == sizeof(yes));
};
template<class Class>
struct HasMember_##NAME<Class, void>
{
typedef char (&yes)[2];
template<unsigned long> struct exists;
template<typename V> static yes Check (exists<sizeof(&V::NAME)>*);
template<typename> static char Check (...);
static const bool value = (sizeof(Check<Class>(0)) == sizeof(yes));
}
用法:只需使用您想要查找的任何成员调用宏:
Usage: Simply invoke the macro with whatever member you want to find:
HasMember(Foo); // Creates a SFINAE `class HasMember_Foo`
HasMember(i); // Creates a SFINAE `class HasMember_i`
现在我们可以使用HasMember_X来检查任何class中的X,如下所示:
Now we can utilize HasMember_X to check X in ANY class as below:
#include<iostream>
struct S
{
void Foo () const {}
// void Foo () {} // If uncommented then type should be mentioned in `HasMember_Foo`
int i;
};
int main ()
{
std::cout << HasMember_Foo<S, void (S::*) () const>::value << "
";
std::cout << HasMember_Foo<S>::value << "
";
std::cout << HasMember_i<S, int (S::*)>::value << "
";
std::cout << HasMember_i<S>::value << "
";
}
捕获:
class不得有重载方法.如果有,那么这个技巧就失败了.即,即使命名成员出现不止一次,结果也会是 false.B 是 S 的基础 &void B::Bar() 存在,则 HasMember_Bar::value 或 HasMember_Bar::value 或 HasMember_Bar::value 将给出 falseclass
must not have overloaded methods. If it has then this trick fails.
i.e. even though the named member is present more than once, the result will be false.B is base of S & void B::Bar () is present, then HasMember_Bar<S, void (B::*)()>::value or HasMember_Bar<S, void (S::*)()>::value or HasMember_Bar<S>::value will give false这篇关于如何检查类中是否存在成员名称(变量或函数),无论是否指定类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
断言失败(size.width>0 && size.height&gAssertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(断言失败(size.width0 amp;amp; size.height0))
在 C++ 中旋转图像而不使用 OpenCV 函数Rotate an image in C++ without using OpenCV functions(在 C++ 中旋转图像而不使用 OpenCV 函数)
OpenCV:处理每一帧OpenCV: process every frame(OpenCV:处理每一帧)
为什么我不能在 openCV 中打开 avi 视频?Why can#39;t I open avi video in openCV?(为什么我不能在 openCV 中打开 avi 视频?)
OpenCV 无法设置 SVM 参数OpenCV unable to set up SVM Parameters(OpenCV 无法设置 SVM 参数)
使用 cvtColor 转换单一颜色Convert a single color with cvtColor(使用 cvtColor 转换单一颜色)