【发布时间】:2018-01-23 16:06:23
【问题描述】:
我想使用自定义分配器从空闲列表中为std::basic_ostringstream 分配内存。这是我想使用的自定义分配器:
template <class Tp>
struct NAlloc {
typedef Tp value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
NAlloc() = default;
template <class T> NAlloc(const NAlloc<T>&) {}
Tp* allocate(std::size_t n) {
n *= sizeof(Tp);
memoryPool *memPool = memoryPool::GetInstance(10);//get memory pool instance
std::cout << "allocating " << n << " bytes\n";
return static_cast<Tp*>(memPool->allocate(n)); //get memory from pool
}
void deallocate(Tp* p, std::size_t n) {
std::cout << "deallocating " << n*sizeof*p << " bytes\n";
memoryPool *memPool = memoryPool::GetInstance(10);
memPool->deallocate(static_cast<void*>(p));//return memory to pool
}
template<typename U>
struct rebind {
typedef NAlloc<U> other;
};
然后,我这样使用它:
typedef std::basic_string<char, std::char_traits<char>, NAlloc<char>> OstringStream;
****问题:****
int main()
{
OstringStream os; //Object creation
os << " Hello, this is OstreamStream class with memory pool"; //here I am getting error
}
错误:'OstringStream {aka std::basic_string
, NAlloc >}' 不是从 'std::basic_ostream<_chart _traits>' 派生的
【问题讨论】:
-
你有什么问题?
-
如果我这样做,OstringStream os, os , NAlloc
>}' 不是从 'std::basic_ostream<_chart _traits>' 派生的 -
你需要edit问题来显示你的问题(而不是添加评论)。请写一个minimal reproducible example 并将其包含在问题的正文中。谢谢。
-
如果您需要帮助,您需要实际提出问题。请把它放在问题中而不是评论中。
-
您是否必须重载
标签: c++11 stringstream allocator ostringstream