你们如何超越STL中分配班的施工方法的违约行为? 下述情况似乎并不可行:
#include <list>
#include <iostream>
#include <memory>
struct MyObj {
MyObj() {
std::cout << "This is the constructor" << std::endl;
}
MyObj(const MyObj& x) {
std::cout << "This is the copy constructor" << std::endl;
}
};
class MyAlloc : public std::allocator <MyObj>{
public:
void construct(pointer p, const_reference t){
std::cout << "Construct in the allocator" << std::endl;
new( (void*)p ) MyObj(t);
}
};
int main(){
MyObj x;
std::list <MyObj,MyAlloc> list(5,x);
}
该方案的回报
This is the constructor
This is the copy constructor
This is the copy constructor
This is the copy constructor
This is the copy constructor
This is the copy constructor
我愿回过来。
This is the constructor
Construct in the allocator
This is the copy constructor
Construct in the allocator
This is the copy constructor
Construct in the allocator
This is the copy constructor
Construct in the allocator
This is the copy constructor
Construct in the allocator
This is the copy constructor