首页 > 后端开发 > C++ > 正文

c++如何实现类型擦除(Type Erasure)_c++ std::any与std::function原理解析

尼克
发布: 2025-12-04 14:41:10
原创
895人浏览过
c++kquote>类型擦除是通过基类虚函数与模板派生类分离接口与具体类型,实现统一接口操作不同类型的机制,如std::any通过base_holder定义类型无关接口,holder模板保存具体值,并在运行时通过多态调用正确方法,从而实现任意类型存储与访问。

c++如何实现类型擦除(type erasure)_c++ std::any与std::function原理解析

类型擦除是 C++ 中一种重要的编程技术,它允许你隐藏具体类型,从而实现更灵活的接口设计。典型的例子包括 std::anystd::functionstd::variant。它们都使用了类型擦除来封装不同类型的数据或行为,对外提供统一的接口。

什么是类型擦除?

类型擦除的核心思想是:在编译期不知道具体类型的情况下,仍然能够存储和操作这些类型。它通过将“实际类型”与“接口”分离来实现。通常做法是:

  • 定义一个统一的接口(如虚函数或多态基类)
  • 用模板生成针对每种类型的实现
  • 在运行时通过指针或引用调用正确的实现

这样使用者无需知道底层类型,就能完成操作。

手动实现一个简单的 std::any 风格类型擦除

std::any 可以保存任意类型的值。我们可以通过基类 + 模板派生类的方式来模拟其实现机制。

立即学习C++免费学习笔记(深入)”;

#include <memory>
#include <typeinfo>
#include <stdexcept>

class any {
private:
    struct base_holder {
        virtual ~base_holder() = default;
        virtual const std::type_info& type() const = 0;
        virtual std::unique_ptr<base_holder> clone() const = 0;
    };

    template<typename T>
    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<base_holder> clone() const override {
            return std::make_unique<holder>(value);
        }
    };

    std::unique_ptr<base_holder> content;

public:
    any() = default;

    template<typename T>
    any(const T& value) : content(std::make_unique<holder<T>>(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<typename T>
    T& get() {
        if (!content || content->type() != typeid(T)) {
            throw std::bad_cast();
        }
        return static_cast<holder<T>&>(*content).value;
    }

    template<typename T>
    const T& get() const {
        if (!content || content->type() != typeid(T)) {
            throw std::bad_cast();
        }
        return static_cast<const holder<T>&>(*content).value;
    }
};
登录后复制

这个简化版的 any 使用多态基类 base_holder 来抹去具体类型。每个类型 T 实例化一个 holder<t></t>,保存真实值并重写虚函数。拷贝时通过 clone() 实现深拷贝。

std::function 的类型擦除原理

std::function<ret></ret> 能包装任何可调用对象(函数指针、lambda、bind 表达式等)。它的实现也依赖类型擦除。

核心思路与上面类似,但关注的是“调用”操作。我们需要:

  • 一个通用调用接口
  • 为每种可调用类型生成具体的执行逻辑
#include <memory>
#include <utility>

template<typename Signature>
class function;

template<typename Ret, typename... Args>
class function<Ret(Args...)> {
private:
    struct callable_base {
        virtual ~callable_base() = default;
        virtual Ret call(Args... args) = 0;
        virtual std::unique_ptr<callable_base> clone() const = 0;
    };

    template<typename F>
    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>(args)...);
        }

        std::unique_ptr<callable_base> clone() const override {
            return std::make_unique<callable_wrapper>(func);
        }
    };

    std::unique_ptr<callable_base> 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<typename F>
    function(F f) : impl(std::make_unique<callable_wrapper<F>>(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>(args)...);
    }
};
登录后复制

这里的关键是把“调用”抽象成虚函数 call()。不同可调用对象被封装进 callable_wrapper<f></f>,各自实现自己的调用逻辑。外部只看到统一的 operator() 接口。

性能与优化考虑

上述实现使用虚函数调用,有间接跳转开销。真实标准库实现会做更多优化:

  • 小对象优化(Small Buffer Optimization):对于小型可调用对象(如普通函数指针、小型 lambda),直接存在对象内部,避免堆分配
  • 函数指针代替虚表:有些实现用函数指针数组代替虚函数,减少虚表查找成本
  • 内联存储:像 libstdc++ 和 libc++ 都会在 std::function 内部预留一段空间存放小闭包

例如,如果 lambda 没有捕获或只捕获少量数据,就不用动态分配内存,提升性能。

总结

C++ 中的类型擦除本质是“用运行时多态模拟泛型能力”。虽然牺牲了一点性能,但换来了极大的灵活性。std::anystd::function 正是这一思想的典型应用。

它们的共同特征是:

  • 对外提供统一接口
  • 内部用模板适配各种类型
  • 通过多态或函数指针实现动态分发
  • 管理对象生命周期(拷贝、移动、销毁)

理解类型擦除有助于深入掌握现代 C++ 库的设计哲学。基本上就这些。

以上就是c++++如何实现类型擦除(Type Erasure)_c++ std::any与std::function原理解析的详细内容,更多请关注php中文网其它相关文章!

c++速学教程(入门到精通)
c++速学教程(入门到精通)

c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号