【发布时间】:2014-10-03 08:14:13
【问题描述】:
请有人帮忙解释为什么在 OS X 上使用 Xcode 5.1 编译以下代码时出现错误。 Apple LLVM 版本 5.1 (clang-503.0.40)(基于 LLVM 3.4svn)。
#include <vector>
#include <functional>
void func1(const std::string& value)
{
// ...
}
void func2(const std::string& value, int min, int max)
{
// ...
}
class X
{
public:
void x1(const std::string& value)
{
// ...
}
void x2(const std::string& value, int min, int max)
{
// ...
}
};
const std::vector<std::function<void(std::string)>> functions
{
func1,
std::bind(func2, std::placeholders::_1, 5, 6),
std::mem_fn(&X::x1), // compiler error
};
报错是:
no matching constructor for initialization of 'const std::vector<std::function<void (std::string)> >'
const std::vector<std::function<void(std::string)>> functions
此外,我想将 X::x2 添加到向量中。我该怎么做?
谢谢。
【问题讨论】:
-
成员函数需要一个对象来操作,在您的情况下,它们的签名与
void(std::string)不兼容。您也许可以使用 bind 使其工作,但您需要一个实例。 -
@Mat 请将此添加为答案。
-
@Mat 请你详细说明一下。
-
成员函数需要一个实例才能被调用,但是您可以将它们的指针保存在向量中。但是由于成员函数具有不同的调用签名,因此非成员函数将它们保存在同一个向量中会导致问题。
标签: c++ c++11 vector stdvector std-function