【发布时间】:2010-06-04 19:19:04
【问题描述】:
我了解以下代码不起作用,因为 i 是运行时参数而不是编译时参数。但我想知道,是否有办法实现同样的目标。我有一个类列表,我需要为每个类调用一个模板函数。
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 并使用该类型调用全局函数,然后使用该参数执行一些运行时操作。
提前致谢, 悟空。
工作代码
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;
}
【问题讨论】: