std::function not compiling in VS2012

后端 未结 1 945
囚心锁ツ
囚心锁ツ 2020-12-20 14:50

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?

The code

         


        
相关标签:
1条回答
  • 2020-12-20 15:16

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

    For now the following works :

    Edit: 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);
    }
    
    0 讨论(0)
提交回复
热议问题