
类型擦除是 C++ 中一种重要的编程技术,它允许你隐藏具体类型,从而实现更灵活的接口设计。典型的例子包括 std::any、std::function 和 std::variant。它们都使用了类型擦除来封装不同类型的数据或行为,对外提供统一的接口。
类型擦除的核心思想是:在编译期不知道具体类型的情况下,仍然能够存储和操作这些类型。它通过将“实际类型”与“接口”分离来实现。通常做法是:
这样使用者无需知道底层类型,就能完成操作。
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<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() 接口。
上述实现使用虚函数调用,有间接跳转开销。真实标准库实现会做更多优化:
std::function 内部预留一段空间存放小闭包例如,如果 lambda 没有捕获或只捕获少量数据,就不用动态分配内存,提升性能。
C++ 中的类型擦除本质是“用运行时多态模拟泛型能力”。虽然牺牲了一点性能,但换来了极大的灵活性。std::any 和 std::function 正是这一思想的典型应用。
它们的共同特征是:
理解类型擦除有助于深入掌握现代 C++ 库的设计哲学。基本上就这些。
以上就是c++++如何实现类型擦除(Type Erasure)_c++ std::any与std::function原理解析的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号