Question

I found detect class 's member use global template function is not work:

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();
};

it fail detect TestBB's foo,is normal? or a complier bug? gcc 4.8.1

Was it helpful?

Solution

The compiler hasn't seen the definition of TestBB at the point of the call to printinfo, only the forward declaration. At that point it doesn't know about any members of TestBB.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top