std :: function在VS2012中不编译 [英] std::function not compiling in VS2012

查看:816
本文介绍了std :: function在VS2012中不编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编译以下代码,该代码取自此处,但我得到一个编译错误。

I am trying to compile the following code taken from here but I am getting a compile error. Does anyone have any ideas what might be wrong?

代码

#include <iostream>
#include <functional>

struct Foo {
    Foo(int num) : num_(num) {}
    void print_add(int i) const { std::cout << num_+i << '\n'; }
    int num_;
};


int main()
{
    // store a call to a member function
    std::function<void(const Foo&, int)> f_add_display = &Foo::print_add;
    Foo foo(314159);
    f_add_display(foo, 1);
}

编译错误:

Error   1   error C2664: 'std::_Func_class<_Ret,_V0_t,_V1_t>::_Set' : 
cannot convert parameter 1 from '_Myimpl *' to 'std::_Func_base<_Rx,_V0_t,_V1_t> *' 

谢谢。

推荐答案

这看起来像是VS2012中的一个错误,我提出了一个错误报告此处

This looks like a bug in VS2012, I have made a bug report here.

现在的工作原理如下:

编辑:根据Xeo的建议编辑使用std :: mem_fn

edited based on Xeo's suggestion to use std::mem_fn

#include <iostream>
#include <functional>

struct Foo {
    Foo(int num) : num_(num) {}
    void print_add(int i) const { std::cout << num_+i << '\n'; }
    int num_;
};

int main()
{
    // store a call to a member function
    std::function<void(const Foo&, int)> f_add_display = std::mem_fn(&Foo::print_add);
    Foo foo(314159);
    f_add_display(foo, 1);
}

这篇关于std :: function在VS2012中不编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆