You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写类型trait判断类型是否符合C++20 NTTP的structural要求

在C++20中,非类型模板参数(NTTP)扩展了支持的新类型,由此引入了structural概念,符合要求的类型如下:

  • 左值引用
  • 整型
  • 指针、成员指针以及空指针
  • 枚举类型
  • 浮点类型
  • 满足以下条件的类:所有基类和非静态数据成员均为公开且不可修改,数据成员和基类的类型也需递归满足structural要求,数组同样适用该规则。

以下是一个无法直接正常工作的尝试实现:

template <auto>
struct nttp_test {};

template <typename T, typename = void>
struct is_structural : std::false_type {};

template <typename T>
struct is_structural<T, std::void_t<nttp_test<T{}>>> : std::true_type {};

template <typename T>
inline constexpr bool is_structural_v = is_structural<T>::value;

目前无法确定该实现是否有效,且对*default initialization(默认初始化)*存在顾虑,同时也无法使用std::declval。如果该需求无法实现,是否需要依赖编译器内置能力?


内容的提问来源于stack exchange,提问作者Desmond Gold

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 14:00:02