BOOST_FUSION_ADAPT_TPL_STRUCT和模板数组大小 [英] BOOST_FUSION_ADAPT_TPL_STRUCT and template array size
问题描述
我想一个C ++模板结构感谢<一个迭代href=\"http://www.boost.org/doc/libs/1_53_0/libs/fusion/doc/html/fusion/adapted/adapt_tpl_struct.html\"相对=nofollow> BOOST_FUSION_ADAPT_TPL_STRUCT 。我的结构包含固定大小的多维数组的大小是模板参数。如果我们考虑到Boost的例子修改,以适应我的问题:
I am trying to iterate over a C++ template structure thanks to BOOST_FUSION_ADAPT_TPL_STRUCT. My structure contains fixed-size multidimensional arrays whose sizes are template parameters. If we consider Boost's example modified to fit my problem:
#include <iostream>
#include <string>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
// Example:
// http://www.boost.org/doc/libs/1_53_0/libs/fusion/doc/html/fusion/adapted/adapt_tpl_struct.html
namespace demo
{
template<typename T, unsigned int SIZE1, unsigned int SIZE2, typename Name, typename Age>
struct employee
{
Name name;
Age age;
T ar[SIZE1][SIZE2];
};
}
// Any instantiated demo::employee is now a Fusion sequence
BOOST_FUSION_ADAPT_TPL_STRUCT(
(T)(SIZE1)(SIZE2)(Name)(Age),
(demo::employee) (T)(SIZE1)(SIZE2)(Name)(Age),
(Name, name)
(Age, age)
(T, ar[SIZE1][SIZE2]))
int main()
{
demo::employee<float, 2, 2, std::string, int> e;
e.name = "Bob";
e.age = 25;
e.ar[0][0] = e.ar[1][0] = 0.1;
e.ar[0][1] = e.ar[1][1] = 0.2;
}
编译失败。此外,它还如果我们只添加一个整数模板参数,甚至没有使用它的数组的大小失败。
The compilation fails. Moreover, it also fails if we only add an integer template parameter without even using it for the array size.
是,即使是可能的 BOOST_FUSION_ADAPT_TPL_STRUCT
?如果不是这样,我应该怎么去呢?
Is that even possible with BOOST_FUSION_ADAPT_TPL_STRUCT
? If not, how should I go about this?
推荐答案
从<一个href=\"http://www.boost.org/doc/libs/1_53_0/libs/fusion/doc/html/fusion/adapted/adapt_tpl_struct.html\"相对=nofollow> DOC :
序列(template_param0)(template_param1)...宣布在模板类型参数的名称使用。
The sequence (template_param0)(template_param1)... declares the names of the template type parameters used.
虽然你有非类型模板参数 尺寸
:
template<typename T, unsigned int SIZE, typename Name, typename Age>
struct employee
您可以将其转换为键入模板参数和使用的 的boost :: MPL :: INT _
作为包装适合随身携带的大小。
You may convert it to type template parameter and use boost::mpl::int_
as wrapper for carry size.
现在,您的code是 编译 。
Now, your code is compiled.
template<int Size>
struct Array
{
template<typename T>
struct Of
{
typedef T type[Size];
};
};
namespace demo
{
template<typename T, typename SIZE, typename Name, typename Age>
struct employee
{
Name name;
Age age;
T ar[SIZE::value];
};
}
// Any instantiated demo::employee is now a Fusion sequence
BOOST_FUSION_ADAPT_TPL_STRUCT(
(T)(SIZE)(Name)(Age),
(demo::employee) (T)(SIZE)(Name)(Age),
(Name, name)
(Age, age)
(typename Array<SIZE::value>::template Of<T>::type, ar))
//...
demo::employee<float, int_<2>, std::string, int> e;
这篇关于BOOST_FUSION_ADAPT_TPL_STRUCT和模板数组大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!