10

ここから取得した次のコードをコンパイルしようとしていますが、コンパイルエラーが発生します。誰かが間違っているかもしれないアイデアを持っていますか?

コード

#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> *' 

ありがとう。

4

1 に答える 1

7

これはVS2012のバグのようです。ここで、バグレポートを作成しました。

今のところ、次の作品:

編集:std::mem_fnを使用するというXeoの提案に基づいて編集

#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);
}
于 2012-10-27T19:32:14.033 に答える