I have this problem:
template<typename T> class Bubu
{
...
int (*comparer)(const T t1, const T t2);
...
public:
Bubu(int (*_comparer)(const T t1, const T t2))
{
comparer = _comparer;
}
};
And in another file:
Bubu<char*> asd(strcmp);
Error:
error C2664: 'Bubu<T>::Bubu(int (__cdecl *)(const T,const T))' :
cannot convert parameter 1 from 'int (__cdecl *)(const char *,
const char *)' to 'int (__cdecl *)(const T,const T)'
I don't understand why. Shouldn't the compiler see a "char*" instead of "T" there?
EDIT: the Ideone.com-ready code:
int asdf(const char* a, const char* b)
{ return 0; }
template class Bubu
{
int (*comparer)(const T t1, const T t2);
public:
Bubu(int (*_comparer)(const T t1, const T t2))
{
comparer = _comparer;
}
};
int main(int argc, char* argv[])
{
Bubu asd(asdf);
}
If
T
is specialized aschar*
,const T
meanschar* const
(i.e. immutable pointer to a mutablechar
), rather thanconst char*
==char const*
(i.e. mutable pointer to an immutablechar
).will compile.
Not sure if this is your problem, but your
*
in your function pointer declaration is in the wrong place (at least compared to what I've ever seen). Instead of:It should be:
I think this does what you want:
When
T
ischar*
,const T
ischar* const
which isn't the same thing asconst char *
. You need:Top level const is ignored for function signatures so
is the same type as
so you're OK on the extra top level const although it doesn't gain you anything over the simpler
int (*comparer)(T t1, T t2);
.