Why boost::bind incompatible with forward declaration?

回眸只為那壹抹淺笑 提交于 2020-01-04 07:49:07

问题


boost::bind is unable to bind parameters declared via a forward declaration.

Can anyone explain why? Is this a boost bug?

Sample code:

#include "boost/function.hpp" 
#include "boost/bind.hpp" 
#include <vector>
#include <iostream>

class B;
class A
{
public:
    A() {}

    void func1(int i)                      { std::cout << __PRETTY_FUNCTION__ << "(" << i << ")\n"; } 
    void func2(const std::string& s)       { std::cout << __PRETTY_FUNCTION__ << "(" << s << ")\n"; }
    void func3(const B& b)       { std::cout << __PRETTY_FUNCTION__ << "(" << "unknown" << ")\n"; } 

    static void dispatch(      std::vector<A>& vect, boost::function<void(A      &)> const& func)
    {
        for ( std::vector<A>::iterator iter = vect.begin();
              iter != vect.end();
              ++iter )
        {
            func(*iter);
        }
    }
};

int main()
{
    std::vector<A> vect(3);

    A::dispatch(vect, boost::bind(&A::func1, _1, 3));
    A::dispatch(vect, boost::bind(&A::func2, _1, "hello"));

    const B* b = NULL;
    A a;
    a.func3( *b ); // compiles and works!!
    A::dispatch(vect, boost::bind(&A::func3, _1, *b)); // does not compile!!
}

Error reported is:

main.cpp:37:52: error: invalid use of incomplete type 'class B'
     A::dispatch(vect, boost::bind(&A::func3, _1, *b)); // does not compile

Live demo: http://coliru.stacked-crooked.com/a/5f9437627fdf3c53


回答1:


That's because bind stores the bound parameters by value.

If you do not want that, wrap them in reference wrappers: boost::ref(x) or boost::cref(x).

A::dispatch(vect, boost::bind(&A::func3, _1, boost::cref(*b))); 

Compiles:

Live On Coliru

#include "boost/function.hpp" 
#include "boost/bind.hpp" 
#include <vector>
#include <iostream>

class B;
class A
{
public:
    A() {}

    void func1(int i)                      { std::cout << __PRETTY_FUNCTION__ << "(" << i << ")\n"; } 
    void func2(const std::string& s)       { std::cout << __PRETTY_FUNCTION__ << "(" << s << ")\n"; }
    void func3(const B& b)       { std::cout << __PRETTY_FUNCTION__ << "(" << "unknown" << ")\n"; } 

    static void dispatch(      std::vector<A>& vect, boost::function<void(A      &)> const& func)
    {
        for ( std::vector<A>::iterator iter = vect.begin();
              iter != vect.end();
              ++iter )
        {
            func(*iter);
        }
    }
};

int main()
{
    std::vector<A> vect(3);

    A::dispatch(vect, boost::bind(&A::func1, _1, 3));
    A::dispatch(vect, boost::bind(&A::func2, _1, "hello"));

    const B* b = NULL;
    A a;
    a.func3( *b ); // compiles and works!!
    A::dispatch(vect, boost::bind(&A::func3, _1, boost::cref(*b))); 
}


来源:https://stackoverflow.com/questions/33667356/why-boostbind-incompatible-with-forward-declaration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!