如何在 Visual Studio 2008 SP1 中使用 std::tr1::mem_fun?

发布于 2024-07-08 07:12:27 字数 1346 浏览 8 评论 0 原文

VS2008 SP1 文档讨论了 std::tr1::mem_fun

那么,为什么当我尝试使用 std::tr1::mem_fun 时,为什么会出现此编译错误?:

'mem_fun' : is not a member of 'std::tr1'

同时,我可以使用 std::tr1::function 没有问题。

这是我尝试编译的示例代码,它应该通过 functionTest 实例上调用 TakesInt

#include "stdafx.h"
#include <iostream>
#include <functional>
#include <memory>

struct Test { void TakesInt(int i) { std::cout << i; } };

void _tmain() 
{
    Test* t = new Test();

    //error C2039: 'mem_fun' : is not a member of 'std::tr1'
    std::tr1::function<void (int)> f =
        std::tr1::bind(std::tr1::mem_fun(&Test::TakesInt), t);
    f(2);
}

我尝试使用 mem_fun 的 tr1 版本,因为使用 std::mem_fun 我的代码也无法编译! 我无法从编译器错误中判断问题是否出在我的代码上,或者是否可以通过使用 tr1 的 mem_fun 来修复。 这对你来说是 C++ 编译器错误(或者也许只是我!)。


更新:对。 答案是正确拼写为 mem_fn!

然而,当我修复这个问题时,代码仍然无法编译。

这是编译器错误:

error C2562: 
'std::tr1::_Callable_obj<_Ty,_Indirect>::_ApplyX' :
  'void' function returning a value

The VS2008 SP1 documentation talks about std::tr1::mem_fun.

So why, when I try and use std::tr1::mem_fun, why do I get this compile error?:

'mem_fun' : is not a member of 'std::tr1'

At the same time, I can use std::tr1::function without problems.

Here is the sample code I am trying to compile, which is supposed to call TakesInt on an instance of Test, via a function<void (int)>:

#include "stdafx.h"
#include <iostream>
#include <functional>
#include <memory>

struct Test { void TakesInt(int i) { std::cout << i; } };

void _tmain() 
{
    Test* t = new Test();

    //error C2039: 'mem_fun' : is not a member of 'std::tr1'
    std::tr1::function<void (int)> f =
        std::tr1::bind(std::tr1::mem_fun(&Test::TakesInt), t);
    f(2);
}

I'm trying to use the tr1 version of mem_fun, because when using std::mem_fun my code doesn't compile either! I can't tell from the compiler error whether the problem is with my code or whether it would be fixed by using tr1's mem_fun. That's C++ compiler errors for you (or maybe it's just me!).


Update: Right. The answer is to spell it correctly as mem_fn!

However when I fix that, the code still doesn't compile.

Here's the compiler error:

error C2562: 
'std::tr1::_Callable_obj<_Ty,_Indirect>::_ApplyX' :
  'void' function returning a value

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如果没结果 2024-07-15 07:12:27

将其更改为:

std::tr1::function<void (int)> f =
    std::tr1::bind(std::tr1::mem_fn(&Test::TakesInt), t, std::tr1::placeholders::_1);
f(2);

绑定器需要 int 参数。 因此,您必须给它一个占位符,它代表生成的函数对象所需的整数参数。

顺便说一句:我不确定你是否已经知道这一点。 但你不需要这个 mem_fn 。 只需将其更改为

std::tr1::function<void (int)> f =
    std::tr1::bind(&Test::TakesInt, t, std::tr1::placeholders::_1);
f(2);

Change it to this:

std::tr1::function<void (int)> f =
    std::tr1::bind(std::tr1::mem_fn(&Test::TakesInt), t, std::tr1::placeholders::_1);
f(2);

The binder requires the int argument. So you have to give it a placeholder which stands for the integer argument that the generated function object needs.

Btw: I'm not sure whether you already know this or not. But you don't need that mem_fn for this. Just change it to

std::tr1::function<void (int)> f =
    std::tr1::bind(&Test::TakesInt, t, std::tr1::placeholders::_1);
f(2);
只怪假的太真实 2024-07-15 07:12:27

我不是 TR1 或 VS2008 方面的专家,但快速谷歌搜索表明您正在寻找的函数是 std::tr1::mem_fn 。 (至少,Boost 在他们的 TR1 实现,这就是 上的详细说明维基百科。)

我不确定为什么旧版本的 mem_fun 会出现编译错误。 如果您发布编译器的相关消息,它可能会帮助我们弄清楚。

I am no expert on either TR1 or VS2008, but a quick googling suggests that the function you're looking for is std::tr1::mem_fn instead. (At least, that's what Boost calls it in their TR1 implementation, and that's how it's detailed on Wikipedia.)

I'm not sure why you're getting a compile error with the old version of mem_fun though. If you post the compiler's message about that, it might help us figure it out.

肥爪爪 2024-07-15 07:12:27

要像这样使用 mem_fun ,您需要完全指定所有模板参数(因为 mem_fun 是一个类,并且不会在类上完成自动模板参数推导)。 另外 mem_fun 只有一个带有 0 个参数的默认构造函数。

没有完整的类定义很难正确。
但我对你想要的最好的选择是:(或接近的东西)

 std::tr1::mem_fun<Test,void (Test::*)(Test*),&Test::TakesInt>()

我认为你正在寻找的是 mem_fn()。 这是一个返回 mem_fun 类型对象的函数。 因为它是一个函数自动完成模板参数推导。

  std::tr1::mem_fn(&Test::TakesInt)

要解决第二个问题,请使用: std::bind1st()

  f=    std::bind1st(std::tr1::mem_fn(&Test::TakesInt), t);

To use mem_fun like that you need to fully specify all the template arguments (as mem_fun is a class and automatic template parameter deduction is not done on classes). Also mem_fun only has a default constructor that takes 0 arguments.

Not having the full class definition it is hard to get correct.
But my best bet at what you wanted would be this: (or something close)

 std::tr1::mem_fun<Test,void (Test::*)(Test*),&Test::TakesInt>()

What I think you are looking for is mem_fn(). This is a function that returns an object of type mem_fun. Because it is a function automatic template parameter deduction is done.

  std::tr1::mem_fn(&Test::TakesInt)

To solve the second problem use: std::bind1st()

  f=    std::bind1st(std::tr1::mem_fn(&Test::TakesInt), t);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文