C++ std::function operator=
C++ std::function operator=
我无法在c++ 11中编译一个简单的程序。你可以在这里看看http://cpp.sh/9muxf。
#include <functional>
#include <iostream>
#include <exception>
#include <tuple>
#include <map>
using namespace std;
typedef int Json;
typedef string String;
class Integer/*: public PluginHelper*/
{
public:
Json display(const Json& in)
{
cout << "bad" << endl;
return Json();
}
map<String, function<Json(Json)>>* getSymbolMap()
{
static map<String, function<Json(Json)>> x;
auto f = bind(&Integer::display, this);
x["display"] = f;
return nullptr;
}
};
问题出现在x["display"] = f;
行
std::function
吗? 你的问题在这里:
auto f = bind(&Integer::display, this);
Integer::display
取Json const&
,不带显式参数绑定它。我的gcc拒绝这样的绑定表达式,但是cpp.sh的编译器和我的clang都允许这个编译,可能不正确,因为语言标准声明:
*INVOKE* (fd, w1, w2, ..., wN)
[font =宋体][要求]是有效的某些值w1, w2,…的表达式, wN,其中N == sizeof...(bound_args)
您可以通过使绑定函数对象f
正确来解决您的问题-只需为Json
参数添加占位符:
auto f = bind(&Integer::display, this, placeholders::_1);
演示Integer::display()
接受一个参数。您应该指定它作为占位符,否则std::bind生成的函子签名将被视为不接受任何参数,这与function<Json(Json)>
的签名不匹配。
auto f = bind(&Integer::display, this, std::placeholders::_1);
// ~~~~~~~~~~~~~~~~~~~~~
x["display"] = f;
生活相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- 库函数需要一个 std::function<void(void)>,如何传入类函数?
- Confusion: decltype vs std::function
- 为什么 std::optional::operator=(U&&) 要求你是非标量类型?
- 为什么 std::function 可以作为 std::not2 的参数?
- 'max'匹配'std::function<const int &(const int &, const int &)>'无过载
- 'operator='已弃用:改用 QDir::setPath()
- 传递给std::function template的template参数究竟代表什么
- 过载'operator new'如何导致无限循环?
- 将带有unique_ptr的可变 lambda 传递给 const&std::function
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- "no matching function for call to 'Vector::Vector'"错误
- 如何在向量中删除 std::function<void()>?
- 编译时二叉搜索树错误的反向迭代器表示"no matching function call for operator=()"
- 从 std::ostream 重载 << 运算符时,为什么编译器会给出"too many parameters for this operator function"错误?
- 为什么需要"std::function::operator=(F &&)"来制作临时的"std::function"?
- std::function operator() 和 std::forward 中发生了什么?
- C++ std::function operator=
- 如何访问通过引用传递给operator= function的对象的私有数据?