为什么 boost::bind 与前向声明不兼容?
Why boost::bind incompatible with forward declaration?
boost::bind
无法绑定通过前向声明声明的参数。
谁能解释一下为什么?这是一个提升错误吗?
示例代码:
#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
那是因为绑定按值存储绑定参数。
如果您不想这样,请将它们包装在参考包装器中:boost::ref(x)
或 boost::cref(x)
。
A::dispatch(vect, boost::bind(&A::func3, _1, boost::cref(*b)));
编译:
#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::bind
无法绑定通过前向声明声明的参数。
谁能解释一下为什么?这是一个提升错误吗?
示例代码:
#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
那是因为绑定按值存储绑定参数。
如果您不想这样,请将它们包装在参考包装器中:boost::ref(x)
或 boost::cref(x)
。
A::dispatch(vect, boost::bind(&A::func3, _1, boost::cref(*b)));
编译:
#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)));
}