放置新的超负荷普通新操作员
Placement new With Overloaded Ordinary new Operator
我有一个类型为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 *)
据我所知,placement-new操作符是由C++实现提供的(或者可能是由<new>
提供的,我也尝试过#include
ing?(,并简单地返回其参数(new
使内存可用,而placement-nnew是程序员说给定内存可用(。
奇怪的是,当上面定义的(普通!(新运算符在类中是而不是时,错误不会发生。事实上,MyOtherType
并没有定义它们,它运行得很好。
问题:怎么回事?我该怎么修?
由于在类中定义了operator new
,因此需要使用全局new
来使用它的放置版本。
#include <new>
...
::new (reinterpret_cast<MyType*>(ptr)) MyType(*this);
相关文章:
- <<操作员在下面的行中工作
- C++ 与操作员不匹配<<
- 操作员C++的模棱两可的过载
- C++中>>操作员过载时出现问题?
- NaN 上的宇宙飞船操作员
- 比根<操作员
- SFINAE不能防止模棱两可的操作员过载吗?
- 什么是现实中的"endl"(或任何输出操纵器)?它是如何实现的,它如何与操作员<<一起工
- 为什么"delete"操作员给我访问权限冲突
- 为什么使操作员成为新的专用会打破 std::shared_ptr?
- 在这种情况下是私有的吗?试图使操作员<<过载
- C++操作员过载>>
- 是否有 C++20 浮点数的包装器,使我能够默认宇宙飞船操作员?
- 与异常处理程序中的操作员<<不匹配
- 复印作业操作员说明
- 超负荷的按钮包裹串流的操作员
- 为什么我超负荷的铸造操作员无法访问私人成员
- (C 14)操作员&lt;&lt;超负荷无法正如智能指针向量所预期的那样工作
- 超负荷操作员身体的奥秘
- 放置新的超负荷普通新操作员