
类型擦除是 C++ 中一种重要的编程技术,它允许你隐藏具体类型,从而实现更灵活的接口设计。典型的例子包括 std::any、std::function 和 std::variant。它们都使用了类型擦除来封装不同类型的数据或行为,对外提供统一的接口。
什么是类型擦除?
类型擦除的核心思想是:在编译期不知道具体类型的情况下,仍然能够存储和操作这些类型。它通过将“实际类型”与“接口”分离来实现。通常做法是:
- 定义一个统一的接口(如虚函数或多态基类)
- 用模板生成针对每种类型的实现
- 在运行时通过指针或引用调用正确的实现
这样使用者无需知道底层类型,就能完成操作。
手动实现一个简单的 std::any 风格类型擦除
std::any 可以保存任意类型的值。我们可以通过基类 + 模板派生类的方式来模拟其实现机制。
立即学习“C++免费学习笔记(深入)”;
#include#include #include class any { private: struct base_holder { virtual ~base_holder() = default; virtual const std::type_info& type() const = 0; virtual std::unique_ptr clone() const = 0; }; template struct holder : base_holder { T value; holder(const T& v) : value(v) {} holder(T&& v) : value(std::move(v)) {} const std::type_info& type() const override { return typeid(T); } std::unique_ptr clone() const override { return std::make_unique (value); } }; std::unique_ptr content; public: any() = default; template any(const T& value) : content(std::make_unique >(value)) {} any(const any& other) : content(other.content ? other.content->clone() : nullptr) {} any& operator=(const any& other) { if (this != &other) { content = other.content ? other.content->clone() : nullptr; } return *this; } any(any&&) = default; any& operator=(any&&) = default; bool has_value() const { return content != nullptr; } const std::type_info& type() const { return content ? content->type() : typeid(void); } template T& get() { if (!content || content->type() != typeid(T)) { throw std::bad_cast(); } return static_cast &>(*content).value; } template const T& get() const { if (!content || content->type() != typeid(T)) { throw std::bad_cast(); } return static_cast &>(*content).value; } };
这个简化版的 any 使用多态基类 base_holder 来抹去具体类型。每个类型 T 实例化一个 holder,保存真实值并重写虚函数。拷贝时通过 clone() 实现深拷贝。
std::function 的类型擦除原理
std::function 能包装任何可调用对象(函数指针、lambda、bind 表达式等)。它的实现也依赖类型擦除。
核心思路与上面类似,但关注的是“调用”操作。我们需要:
- 一个通用调用接口
- 为每种可调用类型生成具体的执行逻辑
#include#include template class function; template class function { private: struct callable_base { virtual ~callable_base() = default; virtual Ret call(Args... args) = 0; virtual std::unique_ptr clone() const = 0; }; template struct callable_wrapper : callable_base { F func; callable_wrapper(F f) : func(std::move(f)) {} Ret call(Args... args) override { return func(std::forward (args)...); } std::unique_ptr clone() const override { return std::make_unique (func); } }; std::unique_ptr impl; public: function() = default; function(const function& other) : impl(other.impl ? other.impl->clone() : nullptr) {} function& operator=(const function& other) { if (this != &other) { impl = other.impl ? other.impl->clone() : nullptr; } return *this; } function(function&&) = default; function& operator=(function&&) = default; template function(F f) : impl(std::make_unique >(std::move(f))) {} explicit operator bool() const { return impl != nullptr; } Ret operator()(Args... args) { if (!impl) throw std::bad_function_call(); return impl->call(std::forward (args)...); } };
这里的关键是把“调用”抽象成虚函数 call()。不同可调用对象被封装进 callable_wrapper,各自实现自己的调用逻辑。外部只看到统一的 operator() 接口。
性能与优化考虑
上述实现使用虚函数调用,有间接跳转开销。真实标准库实现会做更多优化:
- 小对象优化(Small Buffer Optimization):对于小型可调用对象(如普通函数指针、小型 lambda),直接存在对象内部,避免堆分配
- 函数指针代替虚表:有些实现用函数指针数组代替虚函数,减少虚表查找成本
-
内联存储:像 libstdc++ 和 libc++ 都会在
std::function内部预留一段空间存放小闭包
例如,如果 lambda 没有捕获或只捕获少量数据,就不用动态分配内存,提升性能。
总结
C++ 中的类型擦除本质是“用运行时多态模拟泛型能力”。虽然牺牲了一点性能,但换来了极大的灵活性。std::any 和 std::function 正是这一思想的典型应用。
它们的共同特征是:
- 对外提供统一接口
- 内部用模板适配各种类型
- 通过多态或函数指针实现动态分发
- 管理对象生命周期(拷贝、移动、销毁)
理解类型擦除有助于深入掌握现代 C++ 库的设计哲学。基本上就这些。











