C++ function pointers inside templates

2019-06-01 02:34发布

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

4条回答
等我变得足够好
2楼-- · 2019-06-01 02:52

If T is specialized as char*, const T means char* const (i.e. immutable pointer to a mutable char), rather than const char* == char const* (i.e. mutable pointer to an immutable char).

Bubu<const char*> asd(strcmp)

will compile.

查看更多
放荡不羁爱自由
3楼-- · 2019-06-01 02:53

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:

int (comparer*)(const T t1, const T t2);

It should be:

int (*comparer)(const T t1, const T t2);
查看更多
叛逆
4楼-- · 2019-06-01 03:09

I think this does what you want:

#include <cstring>
#include <iostream>

template<class T>
class Bubu {
public:
    typedef int (*Comparator)(T, T);

    Bubu(Comparator inComparator) : mComparator(inComparator) { }

    int compare(T a, T b)
    {
        return mComparator(a, b);
    }

private:
    Comparator mComparator;
};

int main() 
{
    Bubu<const char*> obj(strcmp);
    std::cout << obj.compare("one", "two") << std::endl;
    return 0;
}
查看更多
成全新的幸福
5楼-- · 2019-06-01 03:16

When T is char*, const T is char* const which isn't the same thing as const char *. You need:

 Bubu<const char*> asd(strcmp);

Top level const is ignored for function signatures so

int (*)( const char* const, const char* const );

is the same type as

int (*)( const char*, const char* );

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);.

查看更多
登录 后发表回答