跳转至

ev-spdlog-static-assert-make-unique-array-003

字段 值
项目 gabime/spdlog
固定 commit 79524ddd08a4
路径 include/spdlog/common.h
行段 L383-L387
特性 static_assert
许可证 MIT
代码来源 项目源码
级别 manual
审核 zyuhao7 @ 2026-10-27
测试代码 否
发现方式 manual

用法

在自实现的 make_unique 中用 static_assert 拒绝数组类型

为什么典型

回退实现只能处理单对象,静态断言把数组这种不支持的类型直接报错,而非埋下 delete 不匹配。

摘录

template <typename T, typename... Args>
std::unique_ptr<T> make_unique(Args &&...args) {
    static_assert(!std::is_array<T>::value, "arrays not supported");
    return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}

备注

生产路径;C++14 不可用时的兼容实现,断言守住与标准 make_unique 的差异。

查看固定 commit 源码 · 返回证据索引