C ++将原始表转换为另一个表类型 [英] C++ cast raw table to another table type

查看:140
本文介绍了C ++将原始表转换为另一个表类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写类似于C ++ 11中的 std :: array 类。为了做到这一点我是
声明一个类型 char 在这个类里面的表,后来我想调用
placement new 在这个表上之后,我想使用表作为类型 T 的常规表,这里有麻烦。



一般变量如:

  

类型 char(&)[size] ,如果是这样,我想使用 reinterpret_cast 在这个表上将它转换为类型的表,我实际上使用,所以或多或少我会像我的代码看起来像这样:

  char tab [sizeof(T)* size]; 
T tabT [size] = reinterpret_cast< T(&)[size]>(tab);
//使用tabT更多的代码

但是在这个上下文中, $ c> char * 类型。原因,为什么我认为它可以工作是能够写以下模板函数

 模板< typename T,size_t size& 
函数(T(& table)[size]){
//做与T类型和尺寸大小的表连接的东西。
}

我知道我可以做到这一点没有这个奇特的魔法,要知道,为什么它不工作。



所以我的问题是:有一种方法来做我想做的事情,有什么更优雅的方式上面的工作?



PS:我不声明类型T的原始表: T tab [size] 因为我不能创建元素,没有没有
参数的构造函数。

解决方案

不真正帮助你:

  char标签[sizeof(T)* size]; 
T tabT [size] = reinterpret_cast< T(&)[size]>(tab);

由于数组不可复制,所以代码实例化时不会编译。至少你需要使用

  T(& tabT)[size] = reinterpret_cast< T(& )[Size]>(tab);但是,我建议不要将未初始化的元素存储为 char  



数组开始。只需使用嵌套数组使用 union

  template< typename T,int Size> 
类数组
{
联合数据{
数据(){}
T值[大小];
} data;
public:
array(){/ *适当初始化元素* /}
〜array(){/ *销毁初始化元素* /}
// ...
};


I want to write class similar to the std::array from C++11. To do this I am declaring a table of type char inside this class and later I would like to call placement new on this table After that I would like to use table as if it was regular table of type T and here comes the trouble.

Generally variable like:

char tab[size]; 

Is of type char(&)[size] and if that's so I would like to use reinterpret_cast on this table to cast it to table of type, that I am in fact using, so more or less I would like my code to look like this:

char tab[sizeof(T)*size];
T tabT[size] = reinterpret_cast<T(&)[size]>(tab);
// more code using tabT

However in this context tab is seen as char* type. Reason, why I thought it could work is ability to write following template function

template <typename T, size_t size>
function(T(&table)[size]){
    //do stuff connected with table of type T and size size.
}

I know I could do this without this fancy magic here, but I just want to know, why it does not work.

So my question is: Is there a way to do the thing I want to do and is there any more elegant way to do mentioned job?

PS: I do not declare raw table of type T like : T tab[size], because I wouldn't be able to create elements, where there is no constructor without arguments.

解决方案

The cast doesn't really help you:

char tab[sizeof(T)*size];
T tabT[size] = reinterpret_cast<T(&)[size]>(tab);

Since arrays aren't copyable this code doesn't compile when instantiated. At the very least you'd need to use

T (&tabT)[size] = reinterpret_cast<T(&)[Size]>(tab);

However, I'd recommend not to store the uninitialized elements as char array to start with. Just use a union with a nested array:

template <typename T, int Size>
class array
{
    union Data {
        Data() {}
        T values[Size];
    } data;
public:
    array() { /* initialize the elements appropriately */ }
    ~array() { /* destroy the initialized elements */ }
    // ...
};

这篇关于C ++将原始表转换为另一个表类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆