我知道下面的代码将不起作用,因为我是运行时参数而不是编译时参数。但我想知道,是否有办法达到同样的效果。我有一个类的列表,我需要调用一个模板函数,每个类。C++ boost mpl vector
void
GucTable::refreshSessionParams()
{
typedef boost::mpl::vector< SessionXactDetails, SessionSchemaInfo > SessionParams;
for(int i = 0; i < boost::mpl::size<SessionParams>::value; ++i)
boost::mpl::at<SessionParams, i>::type* sparam =
g_getSessionParam< boost::mpl::at<SessionParams, i>::type >();
sparam->updateFromGucTable(this);
}
}
有人可以建议我一个简单而优雅的方式来执行相同的?我需要遍历mpl :: vector并使用该类型来调用全局函数,然后使用该参数来执行一些运行时操作。
在此先感谢, Gokul。
工作代码
typedef boost::mpl::vector< SessionXactDetails, SessionSchemaInfo > SessionParams;
class GucSessionIterator
{
private:
GucTable& m_table;
public:
GucSessionIterator(GucTable& table)
:m_table(table)
{
}
template< typename U > void operator()(const U&)
{
g_getSessionParam<U>()->updateFromGucTable(m_table);
}
};
void
GucTable::refreshSessionParams()
{
boost::mpl::for_each<SessionParams>(GucSessionIterator(*this));
return;
}
谢谢。有效。 – Gokul 2010-06-04 20:25:20