c++ - 是否可以使用仿函数作为 std::function 的模板参数?

标签 c++ linux windows c++11 porting

我正在尝试查找用于编译此示例 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::function 模板参数可以是“...或其他函数对象”。我在包含多个 std::function 的大型项目上工作,并且项目可以使用 g++ 构建。我正在尝试在 VS2015 下构建它,但此类代码的编译器会提示:

错误 C2027:使用未定义的类型 'std::_Get_function_impl<_Fty>' 和 [ _Fty=我的结构 ]

当我尝试使用 -std=c++11 在 g++ 下编译上面的小示例时,它也提示:

错误:集合 'std::function a' 的类型不完整,无法定义

所以我认为在我们的大型可构建项目中,g++ 可能已经切换了一些提供这种功能的扩展。

最佳答案

可能您混淆了模板参数和要存储的函数对象。我相信你想写的是

std::function<int(int)> foo{mystruct{}};

关于c++ - 是否可以使用仿函数作为 std::function 的模板参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42812405/

相关文章:

c - 在ansi C中模仿ls

objective-c - 为什么数组按引用传递,而结构在 C 中隐式按值传递?

C89:Windows 上的 getaddrinfo()?

string - Windows 批处理 : find a string in file with "[" in the string

c++ - 当一个函数有一个特定大小的数组参数时,为什么它被替换为一个指针?

c++ - 重置 cin 流状态 C++

c++ - Qt 5.2.0 ftp 和 QNetworkAccessManager

c++ - 如何在 Windows 中以编程方式查找动态加载的模块(静态模块)

linux - 关于设置游戏的问题! linux服务器上的应用程序

css - Windows 上的 Firefox 未加载 @FontFace 资源