Я пытаюсь создать контейнер stl типа только для перемещения, который использует собственный распределитель в VStudio 2012.
Проблема в том, что мне кажется, что я должен предоставить функцию построения для распределителя, который, в свою очередь, нуждается в доступе к общедоступному конструктору копии содержащегося типа.
Я либо получаю:
ошибка C2248: 'std::unique_ptr‹_Ty>::unique_ptr': невозможно получить доступ к частному члену, объявленному в классе 'std::unique_ptr‹_Ty>'
or
ошибка C2039: «конструкция»: не является членом «MyAllocator»
Тот же код работает в clang, поэтому я подозреваю, что проблема связана с Microsoft, но может ли кто-нибудь предложить возможный обходной путь?
Это мой код для минимального воспроизведения
#include <memory>
#include <vector>
using namespace std;
template< typename T>
struct MyAllocator
{
typedef T value_type;
typedef value_type* pointer;
typedef value_type& reference;
typedef const value_type* const_pointer;
typedef const value_type& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
template<class t_other>
struct rebind
{
typedef MyAllocator<t_other> other;
};
MyAllocator():m_id(0) {}
MyAllocator(int id):m_id(id){}
template <class T>
MyAllocator(const MyAllocator<T>& other)
:m_id(other.getId())
{
}
T* allocate(std::size_t n)
{
return reinterpret_cast<T*>(malloc(sizeof(T) * n));
}
void deallocate(T* p, std::size_t n)
{
free(p);
}
int getId() const{ return m_id;}
//Have to add these although should not be necessary
void construct(pointer mem, const_reference value)
{
std::_Construct(mem, value);
}
void destroy(pointer mem)
{
std::_Destroy(mem);
}
private:
int m_id;
};
template <class T1, class U>
bool operator==(const MyAllocator<T1>& lhs, const MyAllocator<U>& rhs)
{
return lhs.getId() == rhs.getId() ;
}
template <class T1, class U>
bool operator!=(const MyAllocator<T1>&, const MyAllocator<U>&)
{
return lhs.getId() != rhs.getId();
}
//define a move only type
typedef unique_ptr<uint32_t> MyIntPtr;
//define a container based on MyIntPtr and MyAllocator
typedef vector<MyIntPtr, MyAllocator<MyIntPtr> > MyVector;
int main(int argc, char* argv[])
{
MyAllocator<MyIntPtr> alloc1(1);
MyVector vec(alloc1);
uint32_t* rawPtr = new uint32_t;
*rawPtr = 18;
vec.emplace_back(rawPtr);
return 0;
}
construct
возьметT&&
в качестве второго параметра и перейдет от него. Более того, в C++11 предполагается, чтоconstruct
принимает произвольный набор аргументов и выполняет эквивалентnew(p) T(std::forward(args))
(не уверен, что VC12 достаточно хорошо поддерживает C++11, чтобы разрешить это). Это необходимо для поддержкиemplace
и подобных. - person Igor Tandetnik   schedule 26.08.2015