boost :: any/std :: any是否将小对象存储在原位? [英] Does boost::any / std::any store small objects in-place?
问题描述
要容纳任意大的对象,boost::any
/std::any
当然需要为对象分配堆空间.但是,对于大小小于或等于指针(int,char,bool,...
)的小型类型,any
可以将值直接存储在指针插槽或某些其他内部内存中,而不分配堆空间.但是实现可以做到这一点吗?
To hold arbitrarily large objects, boost::any
/ std::any
surely needs to allocate heap space for objects. However, for small types whose size is less or equal to a pointer (int,char,bool,...
), any
could instead store the value in-place in the pointer slot or in some other in-place memory and not allocate heap space. But does the implementation do this?
我遇到的情况是,我经常将小类型存储在any
中,而有时仅将大类型存储在string
中.代码很热,因此我要问这个问题.如果不执行优化,我可能会采用自己的实施方式来更好地存储小型类型.
I have a scenario where I often store small types in an any
and only sometimes larger types like string
s. The code is quite hot and therefore I am asking the question. If the optimization is not performed, I might be better off with an own implementation that stores small types in-place.
推荐答案
没有保证,但是 [any.class] 那个
实现应避免为包含的小对象使用动态分配的内存. [示例:构造的对象仅包含int的情况. [示例]这种小对象优化只能应用于
is_nothrow_move_constructible_v<T>
为true
的T
类型.
不幸的是,它没有建议应将 small 视为什么,只是说int
应该能够就位存储.
Unfortunately it does not give a recommendation for what should be considered small except to say a int
should be able to be stored in place.
这篇关于boost :: any/std :: any是否将小对象存储在原位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!