c++ - 在标准 (C++11) 的哪个地方说余数运算符仅适用于整数类型?

标签 c++ c++11 language-lawyer

我想要标准 (n3242/3291/3290) 中关于定义余数运算符不适用于浮点类型的位置的指针。

余数运算符%定义在5.6.2

The binary / operator yields the quotient, and the binary % operator yields the remainder from the division of the first expression by the second. If the second operand of / or % is zero the behavior is undefined. For integral operands the / operator yields the algebraic quotient with any fractional part discarded if the quotient a/b is representable in the type of the result, (a/b)*b + a%b is equal to a.

我找不到禁止使用浮点类型的地方。事实上,标准明确地将 integral operands 视为一种特殊情况,可以解释为:该运算符存在于其他非整数类型。

我知道 MSVC 和 GCC 不接受它,我看了很多关于这个问题的答案,但我无法在 C++ 11 标准中获得有效的指针。

最佳答案

C++11, 5.6/2:

The operands of * and / shall have arithmetic or unscoped enumeration type; the operands of % shall have integral or unscoped enumeration type. The usual arithmetic conversions are performed on the operands and determine the type of the result.

(强调我的)

关于c++ - 在标准 (C++11) 的哪个地方说余数运算符仅适用于整数类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25529991/

相关文章:

c++ - 在初始化列表中使用#define

c++ - SDL/C++ : How to make this function short(er)?

c++ - C++ 模板的部分特化 : template parameter not deducible

c++ - Linux 多线程应用程序中的中断生成 SIGSEGV

c++ - SFINAE这里不适用吗?

c++ - dlsym() 解决方法返回类型

c++ - CM_Get_DevNode_Property_Keys 有没有办法获取 key 的名称

c - 为什么主要编译器对 stdint.h 使用 typedef 而对 stdbool.h 使用 #define?

c++ - 为什么我的 SFINAE 表达式不再适用于 GCC 8.2?

c - "producing negative zeroes"在不支持的系统中是什么意思?