[英]C++ template class crashing by uint8_t type only
I've just found this interesting issue in one of my older projects.我刚刚在我的一个旧项目中发现了这个有趣的问题。 I'd defined a class wrapped around an std::shared_ptr to implement a simple dynamic array as you can see below:
我定义了一个围绕 std::shared_ptr 的类来实现一个简单的动态数组,如下所示:
#include <memory>
template<class Type>
class Array
{
size_t m_size;
std::shared_ptr<Type> m_array;
public:
Array()
: m_size( 0 )
, m_array( nullptr, []( Type * p ){ delete[] p; } )
{}
Array( const size_t size )
: m_size( size )
, m_array( new Type[size] {}, []( Type * p ){ delete[] p; } )
{}
Type & operator[]( const size_t & id )
{
return m_array.get()[id];
}
const Type & operator[]( const size_t & id ) const
{
return m_array.get()[id];
}
Type * get()
{
return m_array.get();
}
const Type * get() const
{
return m_array.get();
}
size_t size() const
{
return m_size;
}
Array & operator= ( const Array<Type> array )
{
m_size = array.size();
m_array = array.m_array;
return *this;
};
};
And it worked fine beside uint8_t :它在 uint8_t 旁边运行良好:
Array<int> Int; // good
Array<long> Long; // good
Array<char> Char; // good
Array<int8_t> Int8; // good
Array<uint8_t> Uint8; // segmentation fault
int main()
{
// create scoped array
{
Array<uint8_t> pattern;
}
// do something
return 0;
}
No member function was called beside the constructor.在构造函数旁边没有调用任何成员函数。 Does anyone any idea why happening this?
有谁知道为什么会发生这种情况? The compiler is: gcc9.2.0-rhel7.6-binutils2.32.gold
编译器为:gcc9.2.0-rhel7.6-binutils2.32.gold
Thanks,谢谢,
Using different versions of compiler and libstdc++ caused the problem.使用不同版本的编译器和 libstdc++ 导致了问题。 Issue fixed, thank you for your questions and tips.
问题已解决,感谢您的问题和提示。 They were really helpful.
他们真的很有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.