生成std ::数组类型并存储它们以供运行时使用
我有一个包装器类,该类已在我使用的容器类型上进行模板,这可能是一个大小的std ::阵列:
//! @tparam C the random access container type to be wrapped. E.g.
//! std::vector, or std::array.
template<class C>
class MyWrapper
{
public:
...
//! @return the maximum number of elements that can be stored by the wrapped
//! container.
static size_t cardinality();
}
现在,我可以在编译时使用std ::阵列创建此包装器。到目前为止都很好。问题在于,该包装器将通过网络通过网络发送到另一个模块(使用Protobuf和GRPC序列化)。现在,我可以发送容器,大小和它所携带的数据的类型。例如,如果我有一个类型包装器的对象六个,两倍。
问题在于,在接收器方面,我必须重新创建完全相同的对象。但是我不能拥有一个函数,该函数符合大小和键入参数并在运行时创建数组,例如
template<class T>
createArr(size_t n)
{
MyWrapper<std::array<T, n>> arr;
...
}
,我的想法是我应该能够在编译时生成一些std ::数组类型表并使用此表在运行时创建数组。这将具有我只能支持有限数量的类型和尺寸的约束,例如:
std::array<int, 1>
std::array<int, 2>
...
std::array<int, 1000>
std::array<double, 1>
std::array<double, 2>
...
std::array<double, 1000>
但是我对此表示满意。
是否有任何方法可以使用模板元编程?
编辑:如果我做这样的事情:
template<class T, int n>
createArr()
{
MyWrapper<std::array<T, n>> arr;
...
}
这意味着我必须对每种类型和大小都有一个模板声明,这是我想避免在代码中清楚地写出的东西,因为我有很多类型和尺寸...这就是为什么我认为可以以某种方式生成这些。
I have a wrapper class which is templated on the container type I am using, which may be an std::array of some size:
//! @tparam C the random access container type to be wrapped. E.g.
//! std::vector, or std::array.
template<class C>
class MyWrapper
{
public:
...
//! @return the maximum number of elements that can be stored by the wrapped
//! container.
static size_t cardinality();
}
Now, I can create this wrapper using an std::array at compile time. All good so far. The problem is that this wrapper will then be sent to another module over the network( serialized using protobuf and grpc). Now, I can send the type of the container, size and the data it carries. For instance, if I have an object of type Wrapper<std::array<double, 6>>
, I can send the information about this being a wrapper for an std::array, of size six, which carries doubles.
The problem is that on the receiver side, I have to re-create the exact same object. But I cannot have e.g. a function that takes a size and type argument and creates an array at run time, e.g.
template<class T>
createArr(size_t n)
{
MyWrapper<std::array<T, n>> arr;
...
}
Thus, my idea is that I should be able to generate some std::array types at compile time, store them in a lookup table and use this table to create arrays at runtime. This will have the constraints that I will only be able to support a limited number of types and sizes, e.g.:
std::array<int, 1>
std::array<int, 2>
...
std::array<int, 1000>
std::array<double, 1>
std::array<double, 2>
...
std::array<double, 1000>
but I am okay with that.
Is there any way of doing this, e.g. using template metaprogramming?
EDIT: If I do something like this:
template<class T, int n>
createArr()
{
MyWrapper<std::array<T, n>> arr;
...
}
This means that I will have to have a template declaration for each type and size, something I want to avoid writing out plainly in the code, since I have so many types and sizes...that's why I thought it would be possible to generate these somehow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论