c++ - 任何类型的 unique_ptr 的 static_assert

标签 c++ static-assert

我如何静态断言表达式是 std::unique_ptrstd::unique_ptr<T>对于任何 T .

static_assert (std::is_pointer<decltype(exp)>()), "not a smart pointer")

以上无效。如果没有什么直截了当的,我只对 bool() 感兴趣运算符是为类型定义的。

最佳答案

通过适当的部分特化创建您自己的特征:

template <class T>
struct is_unique_ptr : std::false_type
{};

template <class T, class D>
struct is_unique_ptr<std::unique_ptr<T, D>> : std::true_type
{};

关于c++ - 任何类型的 unique_ptr 的 static_assert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37972394/

相关文章:

c++ - ostream double

c++ - static_assert with nonconstexpr objects in hana's tutorial

c++ - 仅针对具有枚举非类型模板参数的 C++ 模板函数的特化

c++ - 完美转发常量参数以进行持续评估

c++ - HEAP_NO_SERIALIZE 堆中的多线程访问

c++ - 在我的情况下静态类型转换是一个好的设计吗?

c++ - 为什么在构造函数中分配器?

c++ - 如何用宏做static_assert?

c++ - decltype(constexpr 变量)

c++ - 将 std::shared_ptr<Derived> 转换为 const shared_ptr<Base>&