如何使用数组创建标准::函数
How can I create std::function with array?
我正在尝试用整数做函数数组。
我最终使用了这段代码:std::function<void()> func[100];
但是当我尝试像这样写入数组时:
func[option] = draw("optionName", 255);
我收到此错误:
Error C2679 binary '=': no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion
编辑:
现在我使用此代码将函数写入数组:
func[option] = std::bind(draw, "optionName", 255);
但现在它给了我这些错误:
Error C2672 'std::invoke': no matching overloaded function found (compiling source file source.cpp) Project C:Program Files (x86)Microsoft Visual Studio 14.0VCincludetype_traits 1491
Error C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)' (compiling source file source.cpp) Project C:Program Files (x86)Microsoft Visual Studio 14.0VCincludetype_traits 1491
您的代码尝试调用函数并将结果分配给数组。
如果要为函数调用分配特定参数,std::bind
将帮助您做到这一点。
函数模板
bind
为 f 生成转发调用包装器。调用此包装器等效于调用 f 并将其某些参数绑定到 args。
相关文章:
- 编译器如何在使用SFINAE的函数和标准函数之间确定两者是否可行
- 如何使用模板化标准::函数作为参数?
- 使用宏编译时使用用户定义的数学函数,或者仅使用 c++ 中标准数学库中的函数
- C++:带有大括号初始化列表的函数调用表达式 - 标准是否规定在单个元素列表的微不足道的情况下忽略大括号?
- 如何将临时数组传递给标准 C++17 中的函数
- 在双重继承的情况下如何处理非标准构造函数
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- 覆盖程序中的标准 C 库函数C++
- 是否有任何 C 标准函数将值"1"传递给所有 (%s)
- VS2017 #error: : snprintf 的宏定义与标准库函数声明冲突
- 可变参数模板和标准::函数
- 为什么C++标准库容器函数 empty() 标记为 [[nodiscard]]?
- 是否有一个标准函数可以打印/监视stdin文件的内容,同时将数据留在stdin中
- 在Xcode中使用C++17标准库中的bessel数学函数
- 解释标准库排序函数C++的比较谓词的工作原理?
- C++标准是否定义了结构中成员函数的函数内定义是否必须具有静态链接?
- 标准在哪里指定类中定义的函数是内联的?
- C++11:标准::result_of<>模板参数与标准::函数<>
- 为什么我的程序说长度函数不在我的标准库中?
- 错误:调用 to 没有匹配函数...(标准::字符串&)