C++模板内的函数指针

发布于 2024-10-22 19:26:40 字数 935 浏览 0 评论 0原文

我有这个问题:

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

在另一个文件中:

Bubu<char*> asd(strcmp);

错误:

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)'

我不明白为什么。编译器不应该在那里看到“char*”而不是“T”吗?

编辑:Ideone.com 就绪代码:


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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幽蝶幻影 2024-10-29 19:26:40

Tchar* 时,const Tchar* const,这与 不同>const char *。您需要:

 Bubu<const char*> asd(strcmp);

函数签名会忽略顶级 const,因此

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

类型相同,

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

因此您可以使用额外的顶级 const,尽管它不会比更简单的 int (*comparer)(T t1 获得任何好处,T t2);

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

花心好男孩 2024-10-29 19:26:40

如果 T 专门为 char*,则 const T 表示 char* const (即指向可变 const T 的不可变指针) code>char),而不是 const char* == char const* (即指向不可变 char 的可变指针)。

Bubu<const char*> asd(strcmp)

将编译。

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.

始于初秋 2024-10-29 19:26:40

不确定这是否是您的问题,但是函数指针声明中的 * 位于错误的位置(至少与我见过的相比)。而不是:

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

应该是:

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

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);
拥抱我好吗 2024-10-29 19:26:40

我认为这符合你的要求:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文