我有一个使用 boost::mpl 接受策略类列表的类。每个策略类都包含一个标识标签。我希望 MyClass 生成每个策略类的标识标签的 OR-ed 结果。不幸的是,我在弄清楚如何正确使用 boost::mpl::fold<> 功能时遇到了一些麻烦。如果有人可以提供帮助,我将不胜感激。
#include <boost/mpl/vector.hpp>
#include <boost/mpl/bitor.hpp>
#include <boost/mpl/inherit.hpp>
#include <boost/mpl/inherit_linearly.hpp>
namespace bmpl = boost::mpl;
template< class ListOfPolicies >
class CMyClass : public bmpl::inherit_linearly< ListOfPolicies, bmpl::inherit< bmpl::_1, bmpl::_2 > >::type
{
public:
int identifier() const
{
// error C2039: 'tag' : is not a member of 'PolicyA'
return bmpl::fold< ListOfPolicies, bmpl::int_< 0 >, bmpl::bitor_< bmpl::_1, bmpl::_2 > >::value
}
};
template< class T >
class PolicyA
{
public:
enum { MY_IDENTIFIER = 0x00000001 };
};
class PolicyB
{
public:
enum { MY_IDENTIFIER = 0x00000010 };
};
int _tmain(int argc, _TCHAR* argv[])
{
CMyClass< PolicyA, PolicyAB > AB
assert( AB.identifier() == ( PolicyA::MY_IDENTIFIER | PolicyB::MY_IDENTIFIER ));
return 0;
}
谢谢,保罗