标签: comparison-operators

应该在哪里!=运算符在类层次结构中定义?

这是一个非常简单的类层次结构:

class A
{
public:
    A( int _a ) : a( _a ) {}

    virtual bool operator==( const A& right ) const
    {
        return a == right.a;
    }

    virtual bool operator!=( const A& right ) const
    {
        return !( *this == right );
    }

    int a;
};

class B : public A
{
public:
    B( int _a, int _b ) : A( _a ), b( _b ) {}

    virtual bool operator==( const B& right ) const
    {
        return A::operator==( right …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading comparison-operators

7
推荐指数
2
解决办法
398
查看次数

C++20 显式默认相等运算符

我试图了解 C++20 中引入的新默认比较运算符。我的问题是关于何时隐式定义显式默认的比较运算符。下面的代码示例说明了这个问题:

#include <iostream>

struct B
{
  operator bool() const { return true; }
};

struct D : B
{
  bool operator==(const D&) const = default;
};

bool operator==(B, B) { return false; }

int main ()
{ D d;
  std::cout << (d == d);
}

/* Outputs:
0   in gcc 10.1
1   in msvc 19.26
*/
Run Code Online (Sandbox Code Playgroud)

该程序的输出依赖于编译器。似乎 MSVC 在遇到默认声明时为类 D 定义了 operator==,因此它不使用稍后为类 B 定义的 operator==。相比之下,gcc 等待 D 的隐式定义operator== 直到实际需要它,此时为 B 定义的 operator== 在范围内,并被使用。哪种行为(如果有)是正确的?

一个相关的问题是,为什么不能为具有引用成员的类声明默认的 operator== ?我可以看到引用成员可能会对 MSVC 方法造成问题,因为当遇到 …

c++ default operator-overloading comparison-operators c++20

7
推荐指数
1
解决办法
202
查看次数

为什么这个C代码的输出是"否"?

我遇到了这个问题.

#include <stdio.h>

int main(void) {
    // your code goes here
    unsigned int i = 23;
    signed char c = -23;

    if (i > c)
        printf("yes");
    else
        printf("no");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么这段代码的输出是no.

当C intcharC 之间进行比较时,有人可以帮助我理解比较运算符的工作原理吗?

c comparison-operators unsigned-integer

6
推荐指数
1
解决办法
171
查看次数

ASP.NET MVC Razor &lt;text&gt; 标签不接受小于或等于运算符

我有一个 JavaScriptif条件<=>=在局部视图中包含一些比较运算符 ( , )。这个 JavaScript 被 MVC 的 Razor<text>标签包围着。

这样,我的 JS 就会根据模型属性动态加载。但是,如果我在 JavaScript 方法中有比较运算符,则会引发错误。

工作场景:

    @if (Model.SomeTrueCondition)
    {
        <text>
        function JSMethod() {
            AnotherJSMethod();
            return;
        }
        </text>
    }  
Run Code Online (Sandbox Code Playgroud)

不工作场景(如果我调用AnotherJSMethod()using 比较运算符)

    @if (Model.SomeTrueCondition)
    {
        <text>
        function JSMethod() {
            // This if condition containing comparison operators are not being accepted!
            if ($('#aTextBox').val().length <= 0 || $('#bTextBox').val().length <= 0) {
                AnotherJSMethod();
                return;
            }
        }
        </text>
    }
Run Code Online (Sandbox Code Playgroud)

我尝试在另一个 .js 文件中移动这个 JS 方法并尝试嵌入以下方式,但我仍然看到相同的问题。

@section JavaScriptIncludes …
Run Code Online (Sandbox Code Playgroud)

javascript asp.net-mvc comparison-operators razor

6
推荐指数
1
解决办法
1883
查看次数

Python 如何将“int”与“float”对象进行比较?

关于数字类型的文档指出:

Python 完全支持混合算术:当二元算术运算符具有不同数值类型的操作数时,具有“较窄”类型的操作数会被扩展为另一个类型的操作数,其中整数比浮点窄,浮点数比复数窄。混合类型数字之间的比较使用相同的规则。

以下行为支持这一点:

>>> int.__eq__(1, 1.0)
NotImplemented
>>> float.__eq__(1.0, 1)
True
Run Code Online (Sandbox Code Playgroud)

然而,对于大整数,似乎会发生其他事情,因为除非明确转换为,否则它们不会比较相等float

>>> n = 3**64
>>> float(n) == n
False
>>> float(n) == float(n)
True
Run Code Online (Sandbox Code Playgroud)

另一方面,对于 2 的幂,这似乎不是问题:

>>> n = 2**512
>>> float(n) == n
True
Run Code Online (Sandbox Code Playgroud)

由于文档暗示这int是“扩大”(我假设转换/转换?)到float我期望的float(n) == n并且float(n) == float(n)是相似的,但上面的例子有n = 3**64不同的建议。那么什么样的规则不Python中使用比较intfloat(一般或混合数字类型)?


使用 Anaconda 的 CPython 3.7.3 和 PyPy 7.3.0 (Python 3.6.9) 进行测试。

python floating-point types comparison-operators python-3.x

6
推荐指数
1
解决办法
1047
查看次数

为什么 operator!= 从 operator== 合成,而不是相反?

在 c++20 中,如果我提供一个operator==for 类型,那么编译器会合成一个operator!=,但反之则不然。

这是一些代码

struct A {};
bool operator==(A const&, A const&); 

struct B {};
bool operator!=(B const&, B const&); 

int main()
{
    if (A{} != A{}) {}  // error in c++17 
                        // ok in c++20

    if (B{} == B{}) {}  // error in c++17 
                        // error in c++20, why?
}
Run Code Online (Sandbox Code Playgroud)

这似乎不一致,因为!===必须是相反的,如果一个可以从另一个合成,那么逆也应该起作用。这是什么原因?

c++ comparison-operators language-lawyer c++20

6
推荐指数
1
解决办法
185
查看次数

C# 中还有另一个“是”运算符吗?

我正在教我的学生如何编写函数。我使用了一个简单的算法来确定一个 char 值是否是一个实际的字母(而不是一个数字或其他东西)。

所以我实际上输入了我学生所说的(故意装傻):“if(字母是 'A'){...}” 令我惊讶的是,这并没有导致编译器错误。我为 'B' 和 'C' 添加了相同的检查,我的程序现在可以确定 A、B 和 C 确实是字母。

为什么这样做?我到底在比较什么?我没有积极使用类型比较,这正是互联网不断出现的。

我用其他值做了一个额外的实验:

 char l = 'A';
 if (l is 'A')
 {
    Console.WriteLine("l 'A'...");
 }
 if (l is "A")
 {
    // Doesn't compile.
 }

 int x = 15;
 if (x is 15)
 {
    Console.WriteLine("X is 15.");
 }
 if (x is 5.6)
 {
    // Also doesn't compile.
 }
Run Code Online (Sandbox Code Playgroud)

据我所知,“is”函数是相等 (==) 运算符的扩展版本,它也强制执行相同的类型。但我找不到任何关于它的文档。

c# comparison-operators

6
推荐指数
1
解决办法
182
查看次数

operator&lt;=&gt; 是否应该合成数组比较?

我一直在玩我自己的 std::array 实现,并注意到 libc++ 的版本对每个比较(==、!=、<、>、<=、>=)使用明确定义的运算符。我想我可以通过实现 C++20 的飞船操作符 (<=>) 来简化我的代码。但是,当我auto operator<=>(const Array<TYPE,SIZE>&) const = default;在结构体中替换非成员比较运算符时,GCC 主干表示该函数“被隐式删除,因为默认定义格式错误”。一些调查表明原始数组成员是罪魁祸首。

该网页指出,“编译器知道如何将作为数组的类的成员扩展到它们的子对象列表中并递归地比较它们。” 而这个 SO 答案表明只有可复制的数组参与比较综合。

出于好奇,我从编译器资源管理器上的第一个链接运行代码。它也无法在 gcc trunk 上编译。但是,clang trunk 成功编译了代码。

所以,我的问题是:哪个编译器是正确的?是否应该为成员数组合成比较?

c++ arrays comparison-operators c++20

6
推荐指数
1
解决办法
131
查看次数

重写的比较运算符和表达式模板

我有一个有限体积库,受到强烈影响,它可以像在纸上一样用 C++ 编写连续介质力学问题的解决方案。例如,要求解不可压缩层流的 Navier-Stokes 方程,我简单地写:

solve(div(U * U) - nu * lap(U) == -grad(p));
Run Code Online (Sandbox Code Playgroud)

当然,这个表达式涉及表达式模板,因此系统系数是单次计算的,并且系统以无矩阵的方式求解。

这些表达式模板基于 CRTP,因此所有必需的运算符都在基类中定义。特别是,相等运算符的定义如下:

template <typename T>
class BaseExpr {};

template <std::size_t dim, std::size_t rank, typename... TRest, 
          template <std::size_t, std::size_t, typename...> typename T>
class BaseExpr<T<dim, rank, TRest...>>
{
// ...

public:
  template <typename... URest, template <std::size_t, std::size_t, typename...> typename U>
  SystemExpr<dim, rank, T<dim, rank, TRest...>, U<dim, rank, URest...>>
  operator ==(BaseExpr<U<dim, rank, URest...>> const &rhs) const
    { return {*this, rhs}; }

  SystemExpr<dim, rank, T<dim, rank, …
Run Code Online (Sandbox Code Playgroud)

c++ comparison-operators spaceship-operator c++20

6
推荐指数
1
解决办法
211
查看次数

为什么 List&lt;T&gt;.Count 是有符号整数?List&lt;T&gt;.Count 可以为负数吗?

public class List<T>\n{\n     public int Count\n        {\n            get;\n        }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我注意到Count是一个int,结果会小于 0\xef\xbc\x9f

\n

如果Count可以小于0,我必须写\xef\xbc\x9a

\n
if(myList.Count < 1)\n{\n  \n}\n
Run Code Online (Sandbox Code Playgroud)\n

否则我可以这样写:

\n
if(myList.Count == 0)\n{\n  \n}\n
Run Code Online (Sandbox Code Playgroud)\n

.net c# list comparison-operators empty-list

6
推荐指数
1
解决办法
726
查看次数