是否所有类型都需要扩展std :: is_floating_point才能在C ++ 11标准中实现FENV?

时间:2022-11-08 21:23:16

I'm implementing a function as follows:

我正在实现如下函数:

template <typename FP>
void do_something(FP f){
    static_assert(std::is_floating_point<FP>::value, "not a floating point");
    ...
}

In this context, any floating point can be used (native or custom, i.e. multi-precision float libraries).

在此上下文中,可以使用任何浮点(本机或自定义,即多精度浮点库)。

I wonder if standard states anything about what to expect from types overloading the is_floating_point. Can I count for them being required to work with FENV?

我想知道标准是否说明了对重载is_floating_point的类型的期望。我可以指望他们被要求与FENV合作吗?

Can I assume the following code should work properly if a custom floating point implementation is overloading the std::is_floating_point?

如果自定义浮点实现重载std :: is_floating_point,我可以假设以下代码应该正常工作吗?

template <typename FP>
void do_something(FP f){
    static_assert(std::is_floating_point<FP>::value, "not a floating point");
    if (std::fetestexcept(FE_DIVBYZERO)){
        ...
    }
}

1 个解决方案

#1


There ain't no such thing as a "custom floating point type", as far as std::is_floating_point is concerned.

就std :: is_floating_point而言,没有“自定义浮点类型”这样的东西。

20.10.4.1/1 The primary type categories correspond to the descriptions given in section 3.9 of the C++ standard.

20.10.4.1/1主要类型类别对应于C ++标准第3.9节中给出的描述。

Table 47 — Primary type category predicates template <class T> struct is_floating_point; T is a floating point type (3.9.1)

表47 - 主要类型类别谓词模板 struct is_floating_point; T是浮点类型(3.9.1)

3.9.1/8 There are three floating point types: float, double, and long double.

3.9.1 / 8有三种浮点类型:float,double和long double。

17.6.4.2.1/1 The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.

17.6.4.2.1 / 1如果C ++程序在命名空间std或命名空间std中的命名空间中添加声明或定义,则该行为是未定义的,除非另有说明。只有当声明取决于用户定义的类型并且特化符合原始模板的标准库要求且未明确禁止时,程序才可以将任何标准库模板的模板特化添加到命名空间std。

#1


There ain't no such thing as a "custom floating point type", as far as std::is_floating_point is concerned.

就std :: is_floating_point而言,没有“自定义浮点类型”这样的东西。

20.10.4.1/1 The primary type categories correspond to the descriptions given in section 3.9 of the C++ standard.

20.10.4.1/1主要类型类别对应于C ++标准第3.9节中给出的描述。

Table 47 — Primary type category predicates template <class T> struct is_floating_point; T is a floating point type (3.9.1)

表47 - 主要类型类别谓词模板 struct is_floating_point; T是浮点类型(3.9.1)

3.9.1/8 There are three floating point types: float, double, and long double.

3.9.1 / 8有三种浮点类型:float,double和long double。

17.6.4.2.1/1 The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.

17.6.4.2.1 / 1如果C ++程序在命名空间std或命名空间std中的命名空间中添加声明或定义,则该行为是未定义的,除非另有说明。只有当声明取决于用户定义的类型并且特化符合原始模板的标准库要求且未明确禁止时,程序才可以将任何标准库模板的模板特化添加到命名空间std。