可能重複:
Possible for C++ template to check for a function’s existence?確定一個班有一個特定的成員嗎?
我試圖確定閹類型具有一定的成員。這是我的嘗試:
template <typename T,typename U=void>
class HasX
{
public:
static const bool Result=false;
};
template <typename T>
class HasX<T,typename enable_if_c<(sizeof(&T::X)>0)>::type>
{
public:
static const bool Result=true;
};
struct A
{
int X();
};
struct B
{
int Y();
};
int main()
{
cout<<HasX<A>::Result<<endl; // 1
cout<<HasX<B>::Result<<endl; // 0
}
它實際上編譯和工作在海灣合作委員會,但VC在instanciation的點給出。
代碼有問題,還有其他方法可以做到嗎?
重複[可能爲C++模板檢查函數的存在?](http://stackoverflow.com/questions/257288/possible-for-c-template-to-check-for-a-functions-existence ),也許看看[SFINAE來檢查繼承的成員函數](http://stackoverflow.com/questions/1966362/sfinae-to-check-for-inherited-member-functions)。 – 2010-07-08 00:41:56