use*_*007 1 stringstream ostringstream allocator c++11
我想使用自定义分配器从空闲列表中为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;
};
Run Code Online (Sandbox Code Playgroud)
然后,我像这样使用它:
typedef std::basic_string<char, std::char_traits<char>, NAlloc<char>> OstringStream;
Run Code Online (Sandbox Code Playgroud)
****问题:****
int main()
{
OstringStream os; //Object creation
os << " Hello, this is OstreamStream class with memory pool"; //here I am getting error
}
Run Code Online (Sandbox Code Playgroud)
错误:'OstringStream {aka std::basic_string<char, std::char_traits<char>, NAlloc<char> >}' 不是从 'std::basic_ostream<_CharT, _Traits>' 派生的
您的OstringStream类型是 的 typedef std::basic_string,而不是std::basic_ostream。这就是为什么您从operator<<. 左边的操作数必须是从 派生的对象std::basic_ostream,正如错误消息所说的那样。
std::basic_ostream本身根本不使用分配器。它std::basic_streambuf用于所有 I/O。例如,std::ostringstreamuses std::stringbuf,它使用默认的std::allocator。
在 C++11 中,std::basic_ostringstream有一个可选的Allocator模板参数,它向下传递到它的内部std::basic_stringbuf. 所以,你可以这样写typedef:
typedef std::basic_ostringstream<char, std::char_traits<char>, NAlloc<char>> OstringStream;
int main()
{
OstringStream os;
os << " Hello, this is OstringStream with memory pool";
}
Run Code Online (Sandbox Code Playgroud)
在早期的 C++ 版本中,您必须:
定义一个std::basic_stringbuf使用自定义分配器而不是默认分配器的 typedef 。
构造一个std::ostream使用自定义stringbuf类型实例的标准对象。
例如:
typedef std::basic_stringbuf<char, std::char_traits<char>, NAlloc<char> > Stringbuf_NAlloc;
class OstringStream : public Stringbuf_NAlloc, public std::ostream
{
public:
OstringStream() : Stringbuf_NAlloc(std::ios_base::out), std::ostream(this) {}
};
int main()
{
OstringStream os;
os << " Hello, this is OstringStream with memory pool";
}
Run Code Online (Sandbox Code Playgroud)
无论哪种情况,请知道该os.str()方法将不再返回std::string使用默认分配器的标准。它将返回一个std::basic_string使用您的自定义分配器的。这将在尝试将返回值分配给os.str()标准时导致问题std::string,例如:
std::string s = os.str(); // error!
Run Code Online (Sandbox Code Playgroud)
错误:从 'std::__cxx11::basic_ostringstream<char, std::char_traits<char>, NAlloc>::__string_type {aka std::__cxx11::basic_string<char, std::char_traits<char>, NAlloc> 转换}' 到非标量类型 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' 请求
所以要注意这一点。当涉及到混合分配器时,STL 不是很灵活,因此如果您使用自定义分配器,您通常必须将它应用于您从 STL 使用的每种类型的数据容器。
| 归档时间: |
|
| 查看次数: |
1606 次 |
| 最近记录: |