我有一个MyType
类型的对象,由于SSE原因,需要对其进行16字节的对齐。 因此,我编写了一个分配器并重载了new
运算符。 MyType
中的方法:
inline static void* operator new(size_t size) {
awesome::my_allocator<MyType,16> alloc;
return alloc.allocate(size);
}
inline static void* operator new[](size_t size) { return operator new(size); }
inline static void operator delete(void* ptr) {
awesome::my_allocator<MyType,16> alloc;
alloc.deallocate(reinterpret_cast<MyType*>(ptr),0); //last arg ignored in my impl
}
inline static void operator delete[](void* ptr) { operator delete(ptr); }
现在,出于缓存本地化的原因,我需要将一个实例复制构造到特定的64字节对齐的内存中:
void MyType::copy_into(uint8_t* ptr) const {
new (reinterpret_cast<MyType*>(ptr)) MyType(*this);
}
GCC 告诉我:
error: no matching function for call to ‘MyType::operator new(sizetype, MyType*)’
国际刑事法院告诉我:
error : function "MyType::operator new" cannot be called with the given argument list
1> argument types are: (unsigned __int64, MyType *)
据我理解,放置new运算符由C++实现提供(或可能由<new>
提供,我还尝试了 #include
),它只是返回其参数(new
使内存可用,并且放置new是程序员表示给定的内存可用)。有趣的是,当上面定义的(普通的!)new运算符不在类中定义时,该错误就不会发生。事实上,未定义它们的
MyOtherType
工作得很好。问题:是什么原因造成这种情况?我应该如何解决它?
copy_into()
可以检查给定的内存是否为 16 字节对齐。遗憾的是它无法检查给定的内存是否足够大。 - Jerry101