函数式、bind1st 和 mem_fun

2024-01-04

为什么这不能编译?

#include <functional> 
#include <boost/function.hpp> 

class A { 
    A() { 
        typedef boost::function<void ()> FunctionCall; 
        FunctionCall f = std::bind1st(std::mem_fun(&A::process), this); 
    } 
    void process() {} 
};

Errors:

In file included from /opt/local/include/gcc44/c++/bits/stl_function.h:712,
                 from /opt/local/include/gcc44/c++/functional:50,
                 from a.cc:1:
/opt/local/include/gcc44/c++/backward/binders.h: In instantiation of 'std::binder1st<std::mem_fun_t<void, A> >':
a.cc:7:   instantiated from here
/opt/local/include/gcc44/c++/backward/binders.h:100: error: no type named 'second_argument_type' in 'class std::mem_fun_t<void, A>'
/opt/local/include/gcc44/c++/backward/binders.h:103: error: no type named 'first_argument_type' in 'class std::mem_fun_t<void, A>'
/opt/local/include/gcc44/c++/backward/binders.h:106: error: no type named 'first_argument_type' in 'class std::mem_fun_t<void, A>'
/opt/local/include/gcc44/c++/backward/binders.h:111: error: no type named 'second_argument_type' in 'class std::mem_fun_t<void, A>'
/opt/local/include/gcc44/c++/backward/binders.h:117: error: no type named 'second_argument_type' in 'class std::mem_fun_t<void, A>'
/opt/local/include/gcc44/c++/backward/binders.h: In function 'std::binder1st<_Operation> std::bind1st(const _Operation&, const _Tp&) [with _Operation = std::mem_fun_t<void, A>, _Tp = A*]':
a.cc:7:   instantiated from here
/opt/local/include/gcc44/c++/backward/binders.h:126: error: no type named 'first_argument_type' in 'class std::mem_fun_t<void, A>'
In file included from /opt/local/include/boost/function/detail/maybe_include.hpp:13,
                 from /opt/local/include/boost/function/detail/function_iterate.hpp:14,
                 from /opt/local/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:47,
                 from /opt/local/include/boost/function.hpp:64,
                 from a.cc:2:
/opt/local/include/boost/function/function_template.hpp: In static member function 'static void boost::detail::function::void_function_obj_invoker0<FunctionObj, R>::invoke(boost::detail::function::function_buffer&) [with FunctionObj = std::binder1st<std::mem_fun_t<void, A> >, R = void]':
/opt/local/include/boost/function/function_template.hpp:913:   instantiated from 'void boost::function0<R>::assign_to(Functor) [with Functor = std::binder1st<std::mem_fun_t<void, A> >, R = void]'
/opt/local/include/boost/function/function_template.hpp:722:   instantiated from 'boost::function0<R>::function0(Functor, typename boost::enable_if_c<boost::type_traits::ice_not::value, int>::type) [with Functor = std::binder1st<std::mem_fun_t<void, A> >, R = void]'
/opt/local/include/boost/function/function_template.hpp:1064:   instantiated from 'boost::function<R()>::function(Functor, typename boost::enable_if_c<boost::type_traits::ice_not::value, int>::type) [with Functor = std::binder1st<std::mem_fun_t<void, A> >, R = void]'
a.cc:7:   instantiated from here
/opt/local/include/boost/function/function_template.hpp:153: error: no match for call to '(std::binder1st<std::mem_fun_t<void, A> >) ()'

Because bind1st需要一个二元函数对象。但是,您传递一个一元函数对象。 C++03 的函数对象绑定器并不像 boost 或 tr1 中的那样复杂。事实上,它们面临一些基本问题,例如无法处理带有引用参数的函数。

由于您已经使用了 boost,我建议使用boost::bind

FunctionCall f = boost::bind(&A::process, this); // yay!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

函数式、bind1st 和 mem_fun 的相关文章

  • 如何引用 .net 可执行文件中的类?

    IL 反汇编程序显示了我想在项目中使用的 Net 可执行文件中的类 我如何使用我自己项目中的这些类 从 Visual Studio 上的项目添加对该可执行文件的引用 您应该有权访问它定义的公共类 可执行文件是一个像任何其他程序集一样的程序集
  • 在 TPL Dataflow 中,是否可以在创建块之后但使用之前更改 DataflowBlockOptions?

    有效果吗 我想推迟设置 ExecutionDataflowBlockOptions SingleProducerConstrained 属性 直到我准备好将网络链接在一起 因为 我想将创建块及其语义与将网络及其语义链接在一起分开 但据我所知
  • 忽略父进程中的信号

    我正在尝试实现一个 shell 程序 我希望 shell 程序忽略 SIG INT ctrl c 但在我的程序中 子进程也会忽略 SIG INT 信号 但它不应该这样做 因为 exec 应该将子进程带到另一个程序 并且该程序默认情况下应该处
  • 合并多边形的高效算法

    我有一个多边形列表 在这个列表中 一些多边形重叠 或者接触其他多边形 我的任务是合并所有相互重叠或接触的多边形 我有一个union执行此操作的方法 做到这一点最有效的方法是什么 我目前能想到的是循环遍历多边形列表 检查合并列表以查看该多边形
  • 缓存友好的矩阵移位功能

    我想将二维方阵的第一行移到最后一行 所以如果我有一个像A这样的矩阵 我想要得到B 我可以使用两个简单的 for 循环来做到这一点 例如 void shift int M int N int A M N int i j temp for i
  • 改进绩效反思 - 我应该考虑哪些替代方案?

    我需要动态地设置对象上的一堆或属性的值 将其称为传输对象 将在短时间内创建相当数量的此类传输对象并设置其属性 我想避免使用反射 还有其他选择吗 如果是的话 有我可以查看的示例实现吗 Use Delegate CreateDelegate h
  • 从 C# 访问 COM vtable

    C 中有没有办法访问 COM 对象的虚拟方法表以获取函数的地址 经过大量搜索和拼凑不同的部分解决方案后 我弄清楚了如何做到这一点 首先 您需要为您尝试访问的对象定义 COM 组件类 ComImport Guid InterfaceType
  • 使用 C# 启动 Outlook

    我可以让 C 在代码中启动 Outlook 吗 在 VB6 中 我们使用对象 Outlook Application 并编写 Set oOutlook CreateObject Outlook Application Set oNameSp
  • FxCop 和 GAC 疯狂

    当我尝试分析依赖于模式和实践 企业库数据 以及其他 2 0 0 0 的项目时使用 FxCop FxCop 抱怨它不能 定位程序集引用 即使正在分析的应用程序 dll 是根据其编译的此版本及其在 GAC 中 如果我浏览到 GAC 尝试选择相同
  • C#:如何确定坐标是否在美国大陆?

    我正在获取坐标 纬度 经度 我想检查这些坐标是否位于美国大陆 有没有一种简单的方法可以在 C 中实现 我可以将坐标转换为 MGRS 或 UTM 谢谢 哇哦 他们专门为你准备了 http econym org uk gmap states x
  • 不使用 DAO 压缩 Microsoft Access 数据库

    我用CDatabase类开一个ACCDB访问数据库 司机是 T Microsoft Access Driver mdb accdb 我可以打开并使用数据库 已经这样做很多年了 if DatabaseExist m strMDBPath AJ
  • Magento SOAP V2 API - 附加属性设置为空

    几个小时以来 我一直在尝试通过 SOAP V2 API 创建具有附加属性的产品 每当我打电话时就会添加该产品目录产品创建但我随请求发送的附加属性被设置为空 每当我不添加附加属性时 这两个属性都会设置为其默认值 因此我认为这些属性正在发送和接
  • 调用 Console.ReadLine() 的方法的 C# 单元测试

    我想为名为的类的成员函数创建一个单元测试ScoreBoard它存储了一场比赛中排名前五的球员 问题是我为 SignInScoreBoard 正在呼叫Console ReadLine 这样用户就可以输入他们的名字 public void Si
  • 检查字符串中是否存在所有字符值

    我目前正在做这项任务 但我被困住了 目标是读取文件并查找文件中的字符串中是否存在这些字符值 我必须将文件中的字符串与作为参数放入的另一个字符串进行比较 但是 只要每个字符值位于文件中的字符串中 那么它就 匹配 示例 输入和输出 a out
  • 如何声明返回相同类型的 Func Delegate 的 Func Delegate?

    我想编写一个方法 该方法可以完成一些工作 并最终返回另一个与原始方法具有相同签名的方法 这个想法是根据前一个字节值顺序处理字节流 而不进行递归 通过这样调用它 MyDelegate executeMethod handleFirstByte
  • ASP Net Core 属性路由和双正斜杠

    正如所指出的here https stackoverflow com a 20524044 3129340 URL 中包含双斜杠是有效的 我有一个使用属性路由的 ASP Net Core 项目 一个名为GroupController用于处理
  • ld: 无法对非 PE 输出文件执行 PE 操作错误

    我是操作系统编程的新手 我正在读一本书 其中给出了一个简单的内核示例 如下所示 main char video memory 0xb8000 video memory X 为了编译这个名为 kernel c 的文件 我在 Windows 7
  • 使用智能指针在大型对象集合中创建多个索引

    我正在为一个大型对象集合创建多个索引 即使用不同的键 对象可以改变 集合可以缩小和增长 到目前为止我的想法 保留某种指向对象的指针的多个集合 使用set代替map以获得更好的封装 使用 unordered set 可以很好地扩展大型数据集
  • 即使没有任何转换,也违反了 C 中的严格别名?

    How can i and u i在此代码中打印不同的数字 即使i定义为int i u i 我只能假设我在这里触发了 UB 但我不知道具体是如何触发的 ideone演示 http ideone com Gcv5Xm如果我选择 C 作为语言
  • 删除指针后将其设为 NULL 是一个好习惯吗?

    我首先要说的是 使用智能指针 您将永远不必担心这个问题 下面的代码有什么问题 Foo p new Foo use p delete p p NULL 这是由答案和评论 https stackoverflow com questions 19

随机推荐