【问题标题】:How to pass class public member function as a the template parameter?如何将类公共成员函数作为模板参数传递?
【发布时间】:2013-11-06 13:04:50
【问题描述】:

我正在使用 google 稀疏哈希图库。我有以下类模板:

template <class Key, class T,
          class HashFcn = std::tr1::hash<Key>,   
          class EqualKey = std::equal_to<Key>,
          class Alloc = libc_allocator_with_realloc<std::pair<const Key, T> > >
class dense_hash_map {
.....
typedef dense_hashtable<std::pair<const Key, T>, Key, HashFcn, SelectKey,
                        SetKey, EqualKey, Alloc> ht;
.....

};

现在我将自己的类定义为:

class my_hashmap_key_class {

private:
    unsigned char *pData;
    int data_length;

public:
    // Constructors,Destructor,Getters & Setters

    //equal comparison operator for this class
    bool operator()(const hashmap_key_class &rObj1, const hashmap_key_class &rObj2) const;

    //hashing operator for this class
    size_t operator()(const hashmap_key_class &rObj) const;

};

现在我想将my_hashmap_key_class 作为Keymy_hashmap_key_class::operator()(const hashmap_key_class &amp;rObj1, const hashmap_key_class &amp;rObj2) 作为EqualKeymy_hashmap_key_class::operator()(const hashmap_key_class &amp;rObj) 作为HashFcn 传递给dense_hash_map 类作为参数,同时在主函数中使用它作为:

main.cpp:

dense_hash_map<hashmap_key_class, int, ???????,???????> hmap;

将类成员函数作为模板参数传递的正确方法是什么?

我试过像这样传递:

dense_hash_map<hashmap_key_class, int, hashmap_key_class::operator(const hashmap_key_class &rObj1, const hashmap_key_class &rObj2),hashmap_key_class::operator()(const hashmap_key_class &rObj)> hmap;

但由于未检测到运算符,因此出现编译错误。请帮助我意识到我做错了什么。

【问题讨论】:

  • 我猜你希望这些运算符是static。另外,为什么不使用operator== 进行相等比较? (你还是要手动定义)
  • 让运营商static.
  • 或者使用相当奇怪但并非闻所未闻的事实,即您的类是它自己的相等函子,并简单地传递my_hashmap_key_class。请注意,将构造一个实例来执行比较,并且两个参数都不是构造的比较器本身。 HashFn 参数也是如此。我不经常看到它,但它应该仍然可以在您定义此代码时工作。请注意,您的类必须支持默认构造(我假设它支持)。
  • dense_hash_map&lt;my_hashmap_key_class, int, my_hashmap_key_class,my_hashmap_key_class&gt; hmap 应该可以工作。
  • dense_hash_map hmap 工作.........但给编译器警告 -----警告:在添加的符号文件中找不到可加载部分系统在 0x7ffff7ffa000 处提供的 DSO 警告:RTTI 符号未找到 ....继续

标签: c++ class templates hashmap parameter-passing


【解决方案1】:

正如 cmets 中所讨论的,您应该将等式写为 operator==。此外,要么将这些运算符设为静态,要么删除它们的一个参数(“this”指针将是相等性测试的左侧操作数),否则它们将无法按预期工作。

//equal comparison operator for this class
bool operator==(const hashmap_key_class &rObj2) const;

//hashing operator for this class
size_t operator()() const;

然后,您的类就可以使用如下客户端代码了:

my_hashmap_key_class a = ...;
my_hashmap_key_class b = ...;

a == b;   // calls a.operator==(b), i.e. compares a and b for equality
a();      // calls a.operator()(), i.e. computes the hash of a

那么,使用默认模板参数应该没问题。

【讨论】:

  • 那是真的.....但是我对实例化对象或定义成员函数的方式不感兴趣(因为谷歌稀疏哈希码需要我们的类采用特定格式)。而是如何将成员函数用作模板参数并加以利用。
  • “那么,使用默认模板参数应该没问题”是我需要知道的部分以及如何实现它
  • 简单省略参数:dense_hash_map&lt;my_hashmap_key_class, int&gt;应该做,但我无法测试。如果有问题请报告。
  • 它给出了编译错误:未定义的引用`std::tr1::hash::operator()(my_hashmap_key_class) const'
  • 在这种情况下尝试dense_hash_map&lt;my_hashmap_key_class, int, my_hashmap_key_class&gt;
猜你喜欢
  • 1970-01-01
  • 2020-01-01
  • 2015-04-02
  • 1970-01-01
  • 2021-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多