C++如何将方法作为模板参数传递
假设我有一个类 X
:
class X {
// ...
size_t hash() const { return ...; }
};
我想创建一个 std::tr1::unordered_map
我想传入的地方 X::hash()
为 HashFn
。我知道我可以声明我自己的函子对象。我觉得 应该有一种方法可以通过直接传递指向 X::hash() 的指针来做到这一点。
有没有?
Suppose I have a class X
:
class X {
// ...
size_t hash() const { return ...; }
};
I would like to create a std::tr1::unordered_map<X, int, HashFn>
where I want to pass inX::hash()
as HashFn
. I know I can declare my own functor object. I feel that
there should be a way to do this by directly passing a pointer to X::hash()
.
Is there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不;正如您所展示的,您需要一个小的实用程序结构:
然后您可以创建一个
unordered_map
,如下所示:No; as you've shown it, you need a small utility struct:
Then you can create an
unordered_map
like so:不,没有。原因是无论用作 HashFn 的任何内容都必须采用单个参数,该参数是对容器中对象的 const 引用。
X::hash
采用单个参数,该参数是指向容器中对象的 const 指针(this
指针是隐式的第一个参数这种情况),所以单独使用该函数是不可能的。您可能使用一些绑定魔法,使用 boost::lambda 和 boost::bind。我不太确定如何,但它可能看起来像这样:
它创建一个函数对象,它将用指针调用 X::hash 。
No, there isn't. The reason is that whatever is used as your HashFn must take a single argument which is a const reference to an object in the container.
X::hash
takes a single argument which is a const pointer to an object in the container (thethis
pointer is an implicit first argument in this case), so using that function by it self is not possible.You probably use some bind magic, using boost::lambda and boost::bind. I'm not exactly sure how, but it would probably look something like this:
Which creates a function object which will call X::hash with a pointer.
计算哈希值的函数采用一个
Key
类型的参数,而您的函数不采用该参数。因此它的签名一开始就是错误的。由于您想要实现一个函数,而不是一个函子,因此应该如何完成:
将其设为
static
成员函数并将其作为X::hash
传递或 make它是一个免费功能,由您选择。A function which calculates hash value takes one parameter of type
Key
which your function doesn't take. Hence its signature is wrong to begin with.Since you want to implement a function, rather than a functor, here is how it should be done:
Make it
static
member function and pass it asX::hash
or make it a free function, is your choice.不能直接贴,但是可以包起来。最简单的方法是使用
boost::mem_fn()
或标准等效项(如果您的编译器支持):tr1::mem_fn()
(来自 TR1)或std::mem_fn()
(来自 C++11)。编辑:实际上事情没那么简单。
mem_fn()
对于函数参数来说可以正常工作,但由于其返回类型未指定,因此很难用作模板参数。如果您有 C++11 支持,您可以使用 decltype 来查找类型;否则,您可能最好像您提到的那样编写自己的函数对象。You can't directly, but you can wrap it. The easy way to do so is to use
boost::mem_fn()
, or the standard equivalents if your compiler supports them:tr1::mem_fn()
(from TR1) orstd::mem_fn()
(from C++11).EDIT: Actually it's not so simple.
mem_fn()
will work fine for a function parameter, but since its return type is unspecified it's difficult to use as a template parameter. If you have C++11 support you could usedecltype
to find the type; otherwise you're probably best off writing your own function object as you mentioned.