gpt4 book ai didi

C++ 提升 mpl vector

转载 作者:行者123 更新时间:2023-11-30 01:00:12 26 4
gpt4 key购买 nike

我知道以下代码不起作用,因为 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;
}

最佳答案

我只将 MPL 用于 BOOST_AUTO_TEST_CASE_TEMPLATE 的类型集合,所以我的知识非常有限。但是,我猜你可以使用 for_each遍历 MPL 序列。

关于C++ 提升 mpl vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2977077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com