Joe*_*Joe 4 c++ containers placement-new c++11 emplace
我需要实现一个容器来容纳一定数量的元素,并且出于某种原因,它必须在没有任何堆分配的情况下工作.另一个要求是,不应以任何方式复制或移动容器元素.它们必须直接构造到容器分配的内存中.
为此,我决定使用placement new并将内存管理完全委托给容器实现(在drdobbs上找到一些关于placement new的有用信息).
这里有一个运行的例子.(请注意,使用new uint8_t[size]和std::queue仅仅是为了保持示例简单.我的实际代码具有更复杂,无堆的实现.)
到目前为止,这完全有效,因为客户端代码必须通过以下调用将元素放入容器中:
executer.push(new (executer) MyRunnable("Hello", 123));
Run Code Online (Sandbox Code Playgroud)
现在我想删除executer在此声明中重复写入的需要.我宁愿写一些像这样的东西:
executer.pushNew(MyRunnable("Hello", 123));
Run Code Online (Sandbox Code Playgroud)
要么
executer.pushNew(MyRunnable, "Hello", 123);
Run Code Online (Sandbox Code Playgroud)
也许通过提供适当的模板,但我没有写一个(请不要预处理器宏).
我std::allocator在drdobbs找到了一些有用的信息,但不知道如何将它应用到我的问题中(此外,文章是anno 2000,所以不要利用可能的C++ 11优势).
能不能帮助我找到一种不再需要executer两次的方法?
编辑:成功批准Jarod42的答案后,我在这里更新了我正在运行的示例代码.
对于历史,这里是我最初问题的原始示例代码:
#include <iostream>
#include <queue>
class Runnable {
// Runnable should be uncopyable and also unmovable
Runnable(const Runnable&) = delete;
Runnable& operator = (const Runnable&) = delete;
Runnable(const Runnable&&) = delete;
Runnable& operator = (const Runnable&&) = delete;
public:
explicit Runnable() {}
virtual ~Runnable() {}
virtual void run() = 0;
};
class MyRunnable: public Runnable {
public:
explicit MyRunnable(const char* name, int num): name(name), num(num) {}
virtual void run() override {
std::cout << name << " " << num << std::endl;
}
private:
const char* name;
int num;
};
class Executer {
// Executer should be uncopyable and also unmovable
Executer(const Executer&) = delete;
Executer& operator = (const Executer&) = delete;
Executer(const Executer&&) = delete;
Executer& operator = (const Executer&&) = delete;
public:
explicit Executer() {
}
void* allocateEntry(size_t size) {
// this heap allocation is just to keep this example simple
// my real implementation uses it's own memory management instead (blockpool)
return new uint8_t[size];
}
void push(Runnable* entry) {
queue.push(entry);
}
template <typename R> // this don't works
void pushNew(R) {
push(new (*this) R);
}
inline friend void* operator new(size_t n, Executer& executer) {
return executer.allocateEntry(n);
}
void execute() {
while (queue.size() > 0) {
Runnable* entry = queue.front();
queue.pop();
entry->run();
// Now doing "placement delete"
entry->~Runnable();
uint8_t* p = reinterpret_cast<uint8_t*>(entry);
delete[] p;
}
}
private:
// this use of std::queue is just to keep this example simple
// my real implementation uses it's own heap-less queue instead
std::queue<Runnable*> queue {};
};
int main() {
Executer executer;
executer.push(new (executer) MyRunnable("First", 1));
executer.push(new (executer) MyRunnable("Second", 2));
executer.push(new (executer) MyRunnable("Third", 3));
// but want to use it more like one this
//executer.pushNew(MyRunnable("Fifth", 5)); // how to implement it?
//executer.pushNew(MyRunnable, "Sixth", 6); // or maybe for this usage?
executer.execute();
}
Run Code Online (Sandbox Code Playgroud)
Bar*_*rry 16
这有两个问题:
template <typename R> // this don't works
void pushNew(R) {
push(new (*this) R);
}
Run Code Online (Sandbox Code Playgroud)
第一个是由Jarod42回答你想做的:
template <typename R, typename... Ts>
void pushNew(Ts&&... args) {
push(new (*this) R(std::forward<Ts>(args)...));
}
Run Code Online (Sandbox Code Playgroud)
但更重要的new (*this) R是...... 真的很奇怪.看起来你正构建一个R自己!但你不是,你只是用这种语法来调用你的分配器.这可怕地违反了最不惊讶的原则.我花了很长时间才明白发生了什么.
你应该直接使用你的分配器:
template <typename R, typename... Ts>
void pushNew(Ts&&... args) {
void* slot = allocateEntry(sizeof(R));
push(new (slot) R(std::forward<Ts>(args)...));
}
Run Code Online (Sandbox Code Playgroud)
这更容易理解.
Jar*_*d42 11
附:
template <typename R, typename... Ts>
void pushNew(Ts&&... args) {
push(new (*this) R(std::forward<Ts>(args)...));
}
Run Code Online (Sandbox Code Playgroud)
你可以写:
executor.PushNew<MyRunnable>("Hello", 123);
Run Code Online (Sandbox Code Playgroud)
代替
executer.push(new (executer) MyRunnable("Hello", 123));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4036 次 |
| 最近记录: |