specific problem with custom comparators and templates
I'm sorry for asking so much, but I've encountered another problem I have no idea how to solve... From what I gather, gcc fails to resolve myComparator class type, probably because the following code is not compliant with the standard. The question is if I am missing something or is there a workaround for this problem, which wouldn't require too much changes (like interfaces solution)...
template <typename DATA> class myArray
{
template <typename F> void sort (F &comp)
{
// No problems here
}
template <typename T> void sort(void)
开发者_开发技巧 {
T::myComparator cmp; //Error: expected `;' before 'zzz'
// T::template myComparator cmp; also doesn't work
sort(cmp);
}
};
class test
{
public:
class myComparator
{
public:
bool operator() ( const test *t1, const test * t2)
{
// No problems here
}
};
};
void testCmp()
{
myComparator cmp;
cmp.sort<test>();
}
You've to use typename
as:
typename T::myComparator cmp;
//^^^^^^^
typename
is required because myComparator
is a dependent name.
See this C++ FAQ at stackoverflow itself:
"Where and why do I have to put template
and typename
on dependent names?"
精彩评论