yqZ*_*480 14 c++ language-lawyer c++20
cpp 参考(https://en.cppreference.com/w/cpp/concepts/totally_ordered)表示std::totally_ordered<T>,仅当给定左值 a、b 和 c 类型时才进行建模const std::remove_reference_t<T>:
bool(a < b)恰好是bool(a > b)和之一bool(a == b);bool(a < b)和bool(b < c)都为真,则为bool(a < c)真;bool(a > b) == bool(b < a)bool(a >= b) == !bool(a < b)bool(a <= b) == !bool(b < a)于是我想了一下NaN,发现这float句话不符合bool(a > b) == bool(b < a)。但是。std::totally_ordered<float> true我做错了什么吗?
=======
我用这个宏来创建NaN,
#define NAN ((float)(INFINITY * 0.0F))
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
#include <iostream>
#include <concepts>
using namespace std;
int main(int argc, char* argv[])
{
/*
1) std::totally_ordered<T> is modeled only if, given lvalues a, b and c of type const std::remove_reference_t<T>:
Exactly one of bool(a < b), bool(a > b) and bool(a == b) is true;
If bool(a < b) and bool(b < c) are both true, then bool(a < c) is true;
bool(a > b) == bool(b < a)
bool(a >= b) == !bool(a < b)
bool(a <= b) == !bool(b < a)
*/
constexpr bool b = totally_ordered<float>; // true
cout << typeid(NAN).name() << endl; // float
cout << NAN << endl;
cout << b << endl;
cout << "Exactly one of bool(a < b), bool(a > b) and bool(a == b) is true;" << endl;
cout << (NAN < NAN) << endl;
cout << (NAN > NAN) << endl;
cout << (NAN == NAN) << endl;
cout << " If bool(a < b) and bool(b < c) are both true, then bool(a < c) is true;" << endl;
cout << (1.f < 2.f) << endl;
cout << (2.f < NAN) << endl;
cout << (1.f < NAN) << endl;
cout << "bool(a > b) == bool(b < a)" << endl; ////// IT IS FALSE //////
cout << (NAN > 1.f) << endl;
cout << (1.f < NAN) << endl;
cout << "bool(a >= b) == !bool(a < b)" << endl;
cout << (NAN >= 1.f) << endl;
cout << (NAN < 1.f) << endl;
cout << "bool(a <= b) == !bool(b < a)" << endl;
cout << (NAN <= 1.f) << endl;
cout << (NAN > 1.f) << endl;
cout << endl;
}
Run Code Online (Sandbox Code Playgroud)
Nic*_*las 11
概念有语法要求,即存在某些表达式集并且属于提供某些行为的类型。C++20的特性concept可以检测到这些。
概念也有语义要求,即对表达含义的要求,可能是相互关联的。该concept功能(通常)无法检测到这些。如果类型满足句法和语义要求,则可以说它“建模”了一个概念。
对于totally_ordered,float满足该概念的语法要求,但 IEEE754 浮点数 it 不满足语义要求。事实上,C++20 使用符号中的语法与语义划分totally_ordered<float>作为示例。
有些人concept试图通过要求用户明确选择语义要求来解决这个问题。但totally_ordered不是其中之一。
| 归档时间: |
|
| 查看次数: |
733 次 |
| 最近记录: |