我有以下内容:
class Message
{
public:
bool process()
{
return doProcess();
}
protected:
virtual bool doProcess() = 0;
};
class Hello : public Message
{
protected:
bool doProcess()
{
return false;
}
};
typedef typename boost::mpl::map< boost::mpl::pair< boost::mpl::int_< 0 >, Hello > > map;
struct Generator
{
typedef void result_type;
template< typename t_type >
void operator()(std::vector< boost::shared_ptr< Message > >& processors,
t_type p_element)
{
typedef typename boost::mpl::at< map, t_type >::type c_instanceType
boost::shared_ptr< Message > temp(reinterpret_cast< Message* >(new c_instanceType));
p_processors[p_element] = temp;
}
};
然后像这样调用:
class MessageProcessor
{
public:
MessageProcessor() :
m_processors(12)
{
// eventually there will be 12 different messages so I will add
// them to the mpl map and adjust the range
boost::mpl::for_each< boost::mpl::range_c< boost::uint8_t, 0, 1 > >
(
boost::bind(Generator(), boost::ref(m_processors), _1)
}
private:
std::vector< boost::shared_ptr< Message > > m_processors;
};
此代码干净地编译;但是,当函数以后像这样调用时:
m_processors[0]->process();
返回过程的过程函数中的线路上发生了segmenation故障。我正在使用Boost 1.55的GCC 4.8工作。另请注意,这不是整个代码。在与调试器一起行走时,我看到VPTR在调用Doprocess时似乎是无效的,因此似乎不存在儿童班级。关于如何解决此问题的任何想法?
因此,问题似乎是在做AT at&lt;>时实际上找不到类型,而其他内容正在返回而不是Hello Type。看起来boost::for_each
传递的类型是类型,boost::mpl::integral_c<boost::uint8_t, 0>
,它在MPL地图中不存在,因为我将boost::mpl::int_<0>
存储为键。将地图中的密钥类型更改为 boost::mpl::integeral_c< boost::uint8_t, 0 >
不会表现出来并按预期执行。