简单的方法来实现小缓冲区优化任意类型擦除(像std :: function)。 [英] Easy way to implement small buffer optimization for arbitrary type erasure (like in std::function.)

查看:213
本文介绍了简单的方法来实现小缓冲区优化任意类型擦除(像std :: function)。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我倾向于使用类型擦除技术相当多。
它通常看起来像这样:

I tend to use type erasure technique quite a bit. It typically looks like this:

class YetAnotherTypeErasure
{
public:
   // interface redirected to pImpl
private:
   // Adapting function
   template ...
   friend YetAnotherTypeErasure make_YetAnotherTypeErasure (...);

   class Interface {...};

   template <typename Adaptee>
   class Concrete final : public Interface { 
     // redirecting Interface to Adaptee
   };

   std::unique_ptr<Interface> pImpl_; // always on the heap
};

std :: function 但它有一个小的缓冲区优化,因此如果 Concrete< Adaptee> 小于smth并且没有移动操作,它将被存储在其中。有没有一些通用库解决方案做到相当容易?为了强制在编译时只存储小的缓冲区?也许有人提出了标准化的建议?

std::function does something similar, but it has a small buffer optimization, so if Concrete<Adaptee> is smaller than smth and has nothrow move operations, it will be stored in it. Is there some generic library solution to do it fairly easy? For enforcing small buffer only storing at compile time? Maybe something has been proposed for standardisation?

推荐答案

另一方面,您可以从头开始制作自己的解决方案,基于标准库例如 std :: aligned_storage )。

On the other hand, you can just make your own solution from scratch, based on the standard library (e.g. std::aligned_storage). This may still verbose from the view of users, but not too hard.

实际上,我实现了(不是建议) 任何使用此类优化一些相关的实用程序几年前。最近,libstdc ++的 std :: experimental :: any 的实现几乎完全使用了这个方法(但是, __

Actually I implemented (not proposed then) any with such optimization and some related utilities several years ago. Lately, libstdc++'s implementation of std::experimental::any used the technique almost exactly as this (however, __ prefixed internal names are certainly not good for ordinary library users).

我的实现现在使用一些常用助手来处理存储。这些助手轻松实现类型擦除存储策略(至少适合类似任何的东西)。但我仍然对更一般的高级解决方案感兴趣,以简化接口重定向。

My implementation now uses some common helpers to deal with the storage. These helpers do ease to implement the type erasure storage strategy (at least fit for something similar to any enough). But I am still interested in more general high-level solution to simplify the interface redirecting.

这篇关于简单的方法来实现小缓冲区优化任意类型擦除(像std :: function)。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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