Type traits(类型特征/类型萃取)指一组用于在编译期查询、判断或转换“类型信息”的工具(常见于 C++ 模板元编程)。例如判断某个类型是否为整数、是否可拷贝、是否为指针,或获得去掉引用/const 后的类型等。(在不同语言/语境中也可泛指“类型的属性”。)
/taɪp treɪts/
Type traits help the compiler choose the right overload.
类型特征帮助编译器选择正确的重载函数。
Using type traits like std::is_integral and std::remove_reference, you can write templates that adapt to different input types without runtime cost.
使用诸如 std::is_integral 和 std::remove_reference 这类类型特征,你可以编写能适配不同输入类型、且没有运行时开销的模板代码。
type 来自希腊语 typos(印记、模子),引申为“类型、范式”;trait(s) 来自拉丁语 tractus 相关词族(特征、性质)。在现代编程语境中,type traits 作为术语流行于 C++ 模板体系,用来表示“可在编译期获取的类型性质/规则集合”。
std::enable_if、std::decay 等与 type traits 紧密相关的写法)<type_traits> 标头定义并规范了大量 type traits)