为什么我需要std :: get_temporary_buffer? [英] Why do I need std::get_temporary_buffer?

查看:140
本文介绍了为什么我需要std :: get_temporary_buffer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了什么目的,我应该使用 std :: get_temporary_buffer ?标准说明如下:


获取足以存储n个相邻T对象的指针。


我认为缓冲区将被分配在堆栈上,但这不是真的。根据C ++标准,这个缓冲区实际上不是临时的。这个函数对全局函数 :: operator new 有什么好处,它不会构造对象。我是对的,以下语句是等效的?

  int * x; 
x = std :: get_temporary_buffer< int>(10).first;
x = static_cast< int *>(:: operator new(10 * sizeof(int)));

此函数只对语法糖存在吗?为什么它的名称中有 temporary






博士Dobb's Journal,1996年7月01日用于实现算法:


如果没有可分配的缓冲区,请求,算法仍然正常工作,它只是减慢。



解决方案

Stroustrup在C ++编程语言 §19.4.4,SE):


想法是系统可能会保留多个固定 - 大小的缓冲区准备好快速分配,因此对于 n 对象的请求空间可以产生超过 n 的空间。它也可能产生较少,但是,使用 get_temporary_buffer()的一种方法是乐观地要求很多,然后使用发生什么可用。

[...]因为 get_temporary_buffer()是低级的,并且可能被优化用于管理临时缓冲区,它不应该用作 allocator :: allocate()用于获取长期存储。


也开始引入两个函数:


算法通常需要临时空间来执行可接受的操作。


...但似乎没有在任何地方提供临时长期的定义。 p>

anecdote .stackoverflow.com / amzn / click / 0321942043> 从数学到通用编程 提到,Stepanov在原STL设计中提供了一个伪造的占位符实现:

令人惊讶的是,他几年后发现所有提供STL实现的主要供应商仍在使用这个可怕的实现[...]



For what purpose I should use std::get_temporary_buffer? Standard says the following:

Obtains a pointer to storage sufficient to store up to n adjacent T objects.

I thought that the buffer will be allocated on the stack, but that is not true. According to the C++ Standard this buffer is actually not temporary. What advantages does this function have over the global function ::operator new, which doesn't construct the objects either. Am I right that the following statements are equivalent?

int* x;
x = std::get_temporary_buffer<int>( 10 ).first;
x = static_cast<int*>( ::operator new( 10*sizeof(int) ) );

Does this function only exist for syntax sugar? Why is there temporary in its name?


One use case was suggested in the Dr. Dobb's Journal, July 01, 1996 for implementing algorithms:

If no buffer can be allocated, or if it is smaller than requested, the algorithm still works correctly, It merely slows down.

解决方案

Stroustrup says in "The C++ Programming Language" (§19.4.4, SE):

The idea is that a system may keep a number of fixed-sized buffers ready for fast allocation so that requesting space for n objects may yield space for more than n. It may also yield less, however, so one way of using get_temporary_buffer() is to optimistically ask for a lot and then use what happens to be available.
[...] Because get_temporary_buffer() is low-level and likely to be optimized for managing temporary buffers, it should not be used as an alternative to new or allocator::allocate() for obtaining longer-term storage.

He also starts the introduction to the two functions with:

Algorithms often require temporary space to perform acceptably.

... but doesn't seem to provide a definition of temporary or longer-term anywhere.

An anecdote in "From Mathematics to Generic Programming" mentions that Stepanov provided a bogus placeholder implementation in the original STL design, however:

To his surprise, he discovered years later that all the major vendors that provide STL implementations are still using this terrible implementation [...]

这篇关于为什么我需要std :: get_temporary_buffer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆