为什么的boost ::绑定向前声明不兼容? [英] Why boost::bind incompatible with forward declaration?

查看:223
本文介绍了为什么的boost ::绑定向前声明不兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的boost ::绑定无法绑定通过正向声明中声明的参数。

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

任何人能解释为什么吗?这是一个提升的bug?

Can anyone explain why? Is this a boost bug?

样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!!
}

报告的错误是:

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

现场演示: http://coliru.stacked-crooked.com/a/5f9437627fdf3c53

推荐答案

这是因为绑定存储绑定参数的通过值

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

如果你不希望出现这种情况,将它们包装在包装参考:的boost :: REF(X)的boost :: CREF(X)

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))); 

编译:

<大骨节病> 住在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))); 
}

这篇关于为什么的boost ::绑定向前声明不兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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