我有以下代码在 Visual Studio 2008 SP1 下编译和运行良好。
#include <functional>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/utility.hpp>
class NoncopyableObject : public boost::noncopyable
{
public:
NoncopyableObject(int x) : x_(x) {}
int getValue() const {return x_;}
private:
int x_;
};
template<class F>
class MenuItemDispatcher
{
public:
MenuItemDispatcher(F f) : f_(f) { }
void operator ()(NoncopyableObject& w) const
{
// Invoke the functor
f_(w);
}
private:
typedef boost::function1<void,NoncopyableObject&> FUNC;
FUNC f_;
};
void MenuItem()
{
std::cout << "in MenuItem()\n";
}
template<class F>
MenuItemDispatcher<F> MakeMenuItemDispatcher(F f)
{
return MenuItemDispatcher<F>(f);
}
int main()
{
NoncopyableObject obj(7);
MakeMenuItemDispatcher(boost::bind(&MenuItem))(obj);
}
如果我在 main() 中将 boost::bind 更改为 std::tr1::bind,则会出现错误:
错误 C2248:
'boost::noncopyable_::noncopyable::noncopyable'
: 无法访问在类中声明的私有成员'boost::noncopyable_::noncopyable'
。此诊断发生在编译器生成的函数中
'NoncopyableObject::NoncopyableObject(const NoncopyableObject &)'
所以它试图为 NoncopyableObject 生成一个复制构造函数。有人知道为什么会这样吗?MenuItemDispatcher 的调用运算符引用了一个 NoncopyableObject,所以我很难找出问题所在。