2013-11-23 23 views
1

我發現檢測類的成員使用全局模板功能不起作用:type_trait的有成員故障如果使用全局函數

void printinfo(std::true_type) 
{ 
    cout<<"true_type!"; 
} 
void printinfo(std::false_type) 
{ 
    cout<<"false_type!"; 
} 
class TestAA 
{ 
public: 
     void foo(); 
}; 
class TestBB; 
template<typename T,typename =decltype(&T::foo)> 
std::true_type havefoo(T*){return{};} 
std::false_type havefoo(...){return{};} 
int main() 
{ 
    printinfo(havefoo((TestAA*)nullptr)); 
    printinfo(havefoo((TestBB*)nullptr)); 
} 
class TestBB 
{ 
public: 
     void foo(); 
}; 

它無法檢測TestBB的富,是正常的嗎?或編譯器錯誤? gcc 4.8.1

回答

3

在調用printinfo時,編譯器還沒有看到TestBB的定義,只有前向聲明。那時它不知道TestBB的任何成員。