boost :: any/std :: any是否将小对象存储在原位? [英] Does boost::any / std::any store small objects in-place?

查看:132
本文介绍了boost :: any/std :: any是否将小对象存储在原位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要容纳任意大的对象,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 strings. 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>trueT类型.

不幸的是,它没有建议应将 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屋!

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