복사 생성자가 아닌 사용자 정의 할당자를 사용하여 std :: vector의 크기를 조정할 수 있습니까?사용자 정의 할당자를 사용하여 크기 조정
#include <vector>
#include <iostream>
class A{
public:
A(int x){
std::cout<<" new a:"<<this<<" "<< x<<std::endl;
}
};
template<typename T>
class ParamAlloc:public std::allocator<T>{
public:
template<typename U> struct rebind {typedef ParamAlloc other;};
void construct(typename std::allocator<T>::pointer p, typename std::allocator<T>::const_reference val){
new ((void*)p) T(47);
}
void destroy(typename std::allocator<T>::pointer p){
((T*)p)->~T();
}
};
int main(){
ParamAlloc<A> all;
std::vector<A,ParamAlloc<A> > ac(all);
ac.resize(5);
}
하지만 여전히 나는 (A ::를 인스턴스화하려고 인해 컴파일로 컴파일 에러)를 얻을 : 여기
내가 뭘하려합니다.
이 질문과 관련하여 A 클래스의 개체 벡터를 할당하는 가장 좋은 방법은 무엇입니까 (C++ 11 이전 버전). A 개체의 매개 변수는 매개 변수를 사용하지만 A 개체는 복사되지 않아야합니다.
이 장난감의 예에서는 이렇게 할 수 있습니다. 다른 경우에는 (매개 변수가 실제로 생성 시간에 필요합니다) 아니오. –