可以使用函子作为 std::function 的模板参数
Is possible to use functor as template parameter to std::function?
我正在尝试查找用于编译此示例 c++ 代码的 g++ 编译器参数:
#include <stdio.h>
#include <functional>
struct mystruct
{
int a;
int operator()(int y) { return y + 1; }
};
int main()
{
std::function<mystruct> foo;
return 0;
}
在 cpp 引用中写道,std::函数模板参数可以是"...或其他函数对象"。我从事包含多个 std::function 的大型项目,并且可以使用 g++ 构建项目。我正在尝试在VS2015下构建它,但是此类代码的编译器抱怨:
错误 C2027:使用未定义的类型"std::_Get_function_impl<_Fty>" 跟 [ _Fty=肌理结构 ]
当我尝试使用 -std=c++11 在 g++ 下编译上面的小样本时,它也包括:
错误:聚合"std::函数 a"的类型不完整,无法定义
所以我认为在我们的大型可构建项目中,g++ 可能已经切换了一些提供这种功能的扩展。
您可能将
模板参数与要存储的函数对象混淆了。我相信你想写的是
std::function<int(int)> foo{mystruct{}};
答案是函子不能是其他函子的模板参数。感谢所有帖子。
相关文章:
- 为什么 std::function 可以作为 std::not2 的参数?
- 传递给std::function template的template参数究竟代表什么
- 如果模板没有可变参数,则 Lambda 被推导出为 std::function
- 可变参数模板参数扩展 类型为 std::function 的类成员
- std::function<void()> 接受参数
- 在标准中,模板参数的语法在哪里定义,例如,'std::function<int(char)>'?
- C++派生类重载函数(带有 std::function 参数)不可见
- C++ 事件管理器的回调,使用 std::function 和 std:bind 以及派生类作为参数
- 将函数的参数 - 签名从使用 'std::function<T>' 转换为模板参数类型
- C++初始化 std::function 时如何将占位符绑定到引用/引用参数?
- 仅当一个参数中未使用 std::function 时,模板函数替换才有效
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 将另一个类的成员函数传递到 std::function 参数中
- 带有 std::function 的 ADL:可以通过 std::function 参数列表中的类型找到采用 std::function 对象的函数吗?
- 自动类型推导并将 lambda 传递给 std::function 参数
- 如何在模拟期望中调用const std::function<>&参数?
- 将不可复制的闭包对象传递给 std::function 参数
- 将 Python 函数作为 Boost.Function 参数发送
- 二进制泛型lambda不匹配ptr-to-function参数
- 算法函数:将其作为模板或采用std::function参数