c++ template function pointer problem

Started by
3 comments, last by themadme 12 years, 3 months ago
Hi people,

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]

Here is a code snippet



template<class T>
class LinkedList
{
public:

enum SortReturnCode{LOWEST=1, HIGHEST=2, IDENTICAL=4};

typedef SortReturnCode (*SortQueryFncPtr)(const T Data1, const T Data2);

void setSortQuery(SortQueryFncPtr pSortQueryFncPtr)
{
m_pSortQueryFncPtr = pSortQueryFncPtr;
}

private:

SortQueryFncPtr m_pSortQueryFncPtr;
};

struct Data
{
char cTest[255];
};

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.

thanks
Advertisement
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)
Hi,

Thanks for the reply, SiCrane, *high five*

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])
?

Thanks
Hi,

Duh, that's bad of me, I should really read more carefully rather than get frustrated lol.

Thanks for the reply, SiCrane, *high five*
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;
};

template <typename T>
struct ArgType<T *> {
typedef const T * value;
};

// in the class
typedef SortReturnCode (*SortQueryFncPtr)(typename ArgType<T>::value Data1, typename ArgType<T>::value Data2);
Hi,

Ah that would do the trick, thanks very very much :). Never came across this problem before nor have I read about it, learn something new everyday.

Thanks very much

This topic is closed to new replies.

Advertisement