I have a slight problem an having trouble to understand why it's causing a compile error.
error C2664: 'Lex::LinkedList<T>::setSortQuery' : cannot convert parameter 1 from 'Lex::LinkedList<T>::SortReturnCode (__cdecl *)(const Data,const Data)' to 'Lex::LinkedList<T>::SortReturnCode (__cdecl *)(const T,const T)'
with
[
T=Data *
]
None of the functions with this name in scope match the target type[/quote]
static LinkedList<Data*>::SortReturnCode SortTest(const Data *Test1, const Data *Test2)
{
return LinkedList<Data*>::IDENTICAL;
}
int main()
{
LinkedList<Data*> listTest;
listTest.setSortQuery(SortTest);
return 0;
}
If I create a list container that contains a non-pointer value, (T=Data), then there is no error. Could anyone be kind to explain why I retrieve this error.
Because "const T" for T = Data * is "Data * const" not "const Data *". The const binds to the pointer type not the pointed to type. You'd need the function signature to be:
static LinkedList<Data*>::SortReturnCode SortTest(Data * const Test1, Data * const Test2)
That makes sense I guess, I just presumed that it would const binds to the pointed type as it works for referencing type.
So how would one go about making sure it would const binds to the pointed type? I'm guessing that I would change :
[color=#660066]SortQueryFncPtr[color=#666600])([color=#000088]const[color=#000000] T [color=#660066]Data1[color=#666600],[color=#000000] [color=#000088]const[color=#000000] T [color=#660066]Data2[color=#666600])
to
[color=#660066]SortQueryFncPtr[color=#666600])([color=#000088]const[color=#000000] T *[color=#660066]Data1[color=#666600],[color=#000000] [color=#000088]const[color=#000000] T *[color=#660066]Data2[color=#666600])
?
If T is a [type]Data *[/type], then [type]const T *[/type] would be [type]Data * const *[/type], which still isn't quite what you want. One way to address this is to use a type traits class. Something like:
template <typename T>
struct ArgType {
typedef const T value;
};