为什么专门处理type_trait会导致不确定的行为? [英] Why specializing a type_trait could result in undefined behaviour?

查看:99
本文介绍了为什么专门处理type_trait会导致不确定的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据标准§20.10.2/1标头<type_traits>提要[meta.type.synop]:

According to the standard §20.10.2/1 Header <type_traits> synopsis [meta.type.synop]:

1除非另有说明,否则未定义为该子节中定义的任何类模板添加专门化的程序的行为.

1 The behavior of a program that adds specializations for any of the class templates defined in this subclause is undefined unless otherwise specified.

此特定条款与STL应该是可扩展的一般概念相矛盾,并阻止我们扩展类型特征,如以下示例所示:

This specific clause contradicts to the general notion that STL should be expandible and prevents us from expanding type traits as in the example below:

namespace std {
template< class T >
struct is_floating_point<std::complex<T>> : std::integral_constant
         <
         bool,
         std::is_same<float, typename std::remove_cv<T>::type>::value  ||
         std::is_same<double, typename std::remove_cv<T>::type>::value ||
         std::is_same<long double, typename std::remove_cv<T>::type>::value
         > {};
}

实时演示

其中std::is_floating_point被扩展以处理具有基础浮点类型的complex数字.

where std::is_floating_point is expanded to handle complex number with underlying floating point type as well.

  1. 使标准化委员会决定不应该对类型特征进行专门化的原因是什么.
  2. 将来是否有撤消此限制的计划.

推荐答案

对于主要类型类别(is_floating_point是其中之一),存在一个设计不变式:

For the primary type categories, which is_floating_point is one, there is a design invariant:

对于任何给定类型T,正好一个主要类型类别具有 值为true的值成员.

For any given type T, exactly one of the primary type categories has a value member that evaluates to true.

参考:(20.10.4.1主要类型类别[meta.unary.cat])

Reference: (20.10.4.1 Primary type categories [meta.unary.cat])

程序员在检查某些未知的通用类型T时可以依靠通用代码中的该不变式:如果is_class<T>::valuetrue,则我们不需要检查is_floating_point<T>::value.我们保证后者是false.

Programmers can rely on this invariant in generic code when inspecting some unknown generic type T: I.e. if is_class<T>::value is true, then we don't need to check is_floating_point<T>::value. We are guaranteed the latter is false.

这是一个图 代表主要和复合类型特征(此图顶部的叶子是主要类别).

Here is a diagram representing the primary and composite type traits (the leaves at the top of this diagram are the primary categories).

http://howardhinnant.github.io/TypeHiearchy.pdf

如果允许(例如)std::complex<double>is_classis_floating_point都为真,则此有用的不变式将被破坏.程序员将不再能够依靠以下事实:如果is_floating_point<T>::value == true,则T必须是floatdoublelong double之一.

If it was allowed to have (for example) std::complex<double> answer true to both is_class and is_floating_point, this useful invariant would be broken. Programmers would no longer be able to rely on the fact that if is_floating_point<T>::value == true, then T must be one of float, double, or long double.

现在有一些特征,标准会另说",并且允许用户定义类型的专门化. common_type<T, U>是这样的特征.

Now there are some traits, where the standard does "say otherwise", and specializations on user-defined types are allowed. common_type<T, U> is such a trait.

对于主要型和复合型性状,没有计划放宽对这些性状进行特化的限制.这样做会损害这些特性对C ++可以生成的每个单个类型进行精确而唯一的分类的能力.

For the primary and composite type traits, there are no plans to relax the restriction of specializing these traits. Doing so would compromise the ability of these traits to precisely and uniquely classify every single type that can be generated in C++.

这篇关于为什么专门处理type_trait会导致不确定的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆