![](/img/trans.png)
[英]C++ How to convert a big integer in std::string form to byte/char-array/vector
[英]C++ GCC 4.3.2 error on vector of char-array
問題與此漏洞類似
關於在C ++中將數組存儲在std :: vector中的問題
但出於其他原因(請參見下文)。
對於以下C ++示例程序:
#include <vector>
int main(int c_, char ** v_)
{
const int LENGTH = 100;
std::vector<char[LENGTH]> ca_vector;
return 0;
}
GCC 4.2.3可以干凈地編譯。 GCC 4.3.2發出以下錯誤:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4/bits/stl_construct.h: In function ‘void std::_Destroy(_Tp*) [with _Tp = char [100]]’: /usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4/bits/stl_construct.h:103: instantiated from ‘void std::_Destroy(_ForwardIterator, _ForwardIterator) [with _ForwardIterator = char (*)[100]]’ /usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4/bits/stl_construct.h:128: instantiated from ‘void std::_Destroy(_ForwardIterator, _ForwardIterator, std::allocator&) [with _ForwardIterator = char (*)[100], _Tp = char [100]]’ /usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4/bits/stl_vector.h:300: instantiated from ‘std::vector::~vector() [with _Tp = char [100], _Alloc = std::allocator]’ test.cpp:7: instantiated from here /usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4/bits/stl_construct.h:88: error: request for member ‘~char [100]’ in ‘* __pointer’, which is of non-class type ‘char [100]’
原因顯然是在
include/g++-v4/bits/stl_construct.h
template
inline void
_Destroy(_Tp* __pointer)
{ __pointer->~_Tp(); }
我認為這是由於不正確的數組到指針衰減導致的。
我的問題是: 語言標准中是否有什么阻止數組存儲在std :: vector中的? 還是僅僅是該特殊GCC版本中的錯誤?
我確實認為應該對此進行編譯(即4.2.3是正確的)。
謝謝馬丁
是的,標准中有些東西停止使用數組使用Craft C ++ 98 Standard
第23節集裝箱
存儲在這些組件中的對象的類型必須滿足CopyConstructible類型(20.1.3)的要求以及Assignabletypes的其他要求。
其中組件是各種容器
20.1.3包括該類型必須具有析構函數的要求。
我認為它是一個必須復制分配和刪除元素的向量。 C ++如何知道要復制或刪除char []?
不,這是不允許的,就像您所鏈接的問題中不允許這樣做一樣(而且我看不出“原因有何不同”)。 這不是兩個問題中的一個“錯誤”。 或者至少不是編譯器中的錯誤。 只是在您的代碼中。 ;)
您不能將數組存儲在向量中,因為向量要求其元素是可復制構造和可分配的。
最簡單的解決方案是:
std::vector<boost::array<LENGTH> > ca_vector;
這樣,您不必理會數組/指針的故事。 您是否真的想要這個,是另一個問題。
請讓我知道此代碼的介意。 您已經知道,向量也是某種數組,您不必給出初始大小。
1D vector -> std::vector<char*> ca_vector;
2D vector -> std::vector<char*,char*> ca_vector;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.