& 的用途和&&操作员

发布于 2024-10-20 06:30:33 字数 334 浏览 2 评论 0原文

同样的问题也适用于 |和||。

& 的用途是什么?和&&操作员?我能想到的唯一用途是

使用 & 对 int 基本类型(但不是浮点/小数)进行按位与运算 返回 bool 的 bool/函数的逻辑短路。使用 &&运营商通常。

我想不出我用过它的任何其他案例。
有谁知道其他用途吗?

-编辑- 为了澄清,我问的是任何语言。我看到 DateTime 使用“-”返回时间跨度,字符串使用“+”创建新字符串等。我不记得任何使用 && 的自定义数据类型。和&。所以我想问它们(合理地)可以用来做什么?我不知道有什么例子。

Same question goes for | and ||.

What are uses for the & and && operator? The only use i can think of are

Bitwise Ands for int base types (but not float/decimals) using &
logical short circuit for bools/functions that return bool. Using the && operator usually.

I cant think of any other cases i have used it.
Does anyone know other uses?

-edit- To clarify, i am asking about any language. I seen DateTime use '-' to return a timespan, strings use '+' to create new strings, etc. I dont remember any custom datatype using && and &. So i am asking what might they (reasonably) be use for? I dont know of an example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

合约呢 2024-10-27 06:30:33

在大多数基于 C 的语言中,这些运算符的含义为:

  • && - 布尔 AND。用于布尔表达式,例如 if 语句。
  • || - 布尔或。用于布尔表达式,例如 if 语句。
  • & - 按位与。用于对两个操作数的位进行AND
  • | - 按位或。用于对两个操作数的位进行“或”运算。

然而,这些不能保证一定如此。由于每种语言都定义了自己的运算符,因此这些字符串可以定义为不同语言中的任何内容。

从您的编辑来看,您似乎正在使用 C#。上面的描述适用于 C#,其中 |& 也是条件运算符(取决于上下文)。

至于您所说的 DateTime+ 运算符 - 这与您提到的其他运算符及其含义无关。

In most C-based languages the meanings of these operators are:

  • && - boolean AND. Used in boolean expressions such as if statements.
  • || - boolean OR. Used in boolean expressions such as if statements.
  • & - bitwise AND. Used to AND the bits of both operands.
  • | - bitwise OR. Used to OR the bits of both operands.

However, these are not guaranteed to be such. Since every language defines its own operators, these string can be defined as anything in a different language.

From your edit, you seem to be using C#. The above description is right for C#, with | and & also being conditional operators (depending on context).

As for what you are saying about DateTime and the + operator - this is not related to the other operators you mentioned and their meaning.

无戏配角 2024-10-27 06:30:33

如果您询问所有语言,那么我认为谈论“ & 运算符”是不合理的。标记 & 在不同的语言、运算符和其他语言中可以具有各种含义。

例如,仅在 C 语言中就有两个不同的 & 运算符(一元地址与和二进制按位与)。 C 和相关语言中的一元 & 是我能立即想到的唯一一个我遇到的符合您的标准的例子。

然而,C++ 添加了运算符重载,以便它们可以对用户定义的类表示任何您喜欢的含义,此外 & 字符在类型声明中具有含义。在 C++0x 中,&& 标记在类型声明中也有意义。

APL 或 J 之类的语言可以“合理地”使用 & 运算符来表示几乎任何内容,因为人们不希望这些语言中的代码与类 C 语言有任何相似之处语言。不确定这两者中的任何一个实际上是否使用 &&&

在 C++ 中,二元 & 运算符重载的“合理”含义取决于个人喜好 - 通常在某些情况下它类似于按位 &方式,因为您的类表示的值在某种程度上可以被视为一个位序列。但不一定是这样,只要它在该领域有意义即可。通常,仅仅因为 & 碰巧未被使用而使用 & 重载是相当“不合理”的。但是,如果你的类代表了数学中相当深奥的东西,并且你需要在 +* 之后的第三个二元运算符,我想你会开始四处寻找。如果您想要的是比 + 优先级更低的内容,则二进制 & 是一个候选者。我暂时想不出抽象代数中有任何结构需要这样的东西,但这并不意味着不存在。

在 C++ 中重载 operator&& 具有一定的反社会性,因为未重载版本的运算符会短路,而重载版本则不会。 C++ 程序员习惯于编写像 if (p && *p != 0) 这样的表达式,因此通过重载 operator&& 你实际上是在搞乱控制结构。

在 C++ 中重载一元 operator& 是极其反社会的。它可以阻止人们指向您的对象。 IIRC 有一些尴尬的情况,标准模板的常见实现需要其模板参数,一元 operator& 结果是一个指针(或者至少是一个非常类似指针的东西)。这没有记录在参数的要求中,但是当库编写者开始实现模板时,这几乎或完全不可避免。因此,重载会对类的使用施加限制,而这些限制无法从标准中推断出来,最好有一个很好的理由。

[编辑:当我写这篇文章时我不知道,但现在知道的是,模板编写者可以解决使用带有模板参数的一元 operator& 的需要,而标准不这样做指定 & 对该类型(即所有类型)的作用。您可以执行 boost::addressof 的操作,即:

reinterpret_cast<Foo*>(&reinterpret_cast<char&>(foo))

该标准不需要太多 reinterpet_cast,但由于我们正在讨论标准模板,因此他们确切地知道什么它在实现中确实如此,无论如何将对象重新解释为字符是合法的。我认为这是保证有效的 - 但如果没有,实现可以确保它确实有效,如果有必要编写完全符合标准的模板。

但是,如果您的实现没有达到这些长度以避免调用重载的 operator&,那么原始问题仍然存在。]

If you're asking about all languages then I don't think it's reasonable to talk about "the & operator". The token & could have all sorts of meanings in different languages, operator and otherwise.

For example in C alone there are two distinct & operators (unary address-of and binary bitwise-and). Unary & in C and related languages is the only example I can immediately think of, of a use I've encountered that meets your criteria.

However, C++ adds operator overloading so that they can mean anything you like for user-defined classes, and in addition the & character has meaning in type declarations. In C++0x the && token has meaning in type declarations too.

A language along the lines of APL or J could "reasonably" use an & operator to mean pretty much anything, since there is no expectation that code in those languages bears any resemblance at all to C-like languages. Not sure if either of those two does in fact use either & or &&.

What meanings it's "reasonable" for a binary & operator overload to have in C++ is a matter of taste - normally it would be something that's analogous to bitwise & in some way, because the values represented by your class can be considered as a sequence of bits in some way. Doesn't have to be, though, as long as it's something that makes sense in the domain. Normally it's fairly "unreasonable" to use an & overload just because & happens to be unused. But if your class represents something fairly abstruse in mathematics and you need a third binary operator after + and *, I suppose you'd start looking around. If what you want is something with even lower precedence than +, binary & is a candidate. I can't for the moment think of any structures in abstract algebra that want such a thing, but that doesn't mean there aren't any.

Overloading operator&& in C++ is moderately antisocial, since the un-overloaded version of the operator short-circuits and overloaded versions don't. C++ programmers are used to writing expressions like if (p && *p != 0), so by overloading operator&& you're in effect messing with a control structure.

Overloading unary operator& in C++ is extremely antisocial. It stops people taking pointers to your objects. IIRC there are some awkward cases where common implementations of standard templates require of their template parameters that unary operator& results in a pointer (or at least a very pointer-like thing). This is not documented in the requirements for the argument, but is either almost or completely unavoidable when the library-writer comes to implement the template. So the overload would place restrictions on the use of the class that can't be deduced from the standard, and there'd better be a very good reason for that.

[Edit: what I didn't know when I wrote this, but do know now, is that template-writers could work around the need to use unary operator& with template parameters where the standard doesn't specify what & does for that type (i.e. all of them). You can do what boost::addressof does, which is:

reinterpret_cast<Foo*>(&reinterpret_cast<char&>(foo))

The standard doesn't require much of reinterpet_cast, but since we're talking about standard templates they know exactly what it does in the implementation, and anyway it's legal to reinterpret an object as chars. I think this is guaranteed to work - but if not the implementation can ensure that it does work if necessary to write fully conforming standard templates.

But, if your implementation doesn't go to these lengths to avoid calling an overloaded operator&, the original problem remains.]

骄傲 2024-10-27 06:30:33

由于您之前关于这些运算符的问题是关于 C# 的,我认为这个问题也是关于 C# 的。

通常,您希望使用条件运算符的短路版本来避免不必要的操作。如果第一个操作数的值足以确定结果,则无需计算第二个操作数。

当条件依赖于先前的条件为真时,只有短路运算符起作用,例如进行空检查和属性比较:

if (myObj != null && myObj.State == "active")

在这种情况下使用 & 运算符不会保留第二个操作数被评估,并且会导致空引用异常。

当您希望始终对两个操作数求值时,非短路运算符非常有用,例如当它们有副作用时:

if (DoSomeWork() & DoOtherWork())

使用 && 运算符会阻止如果第一个方法返回 false,则调用第二个方法。

&| 也是二元运算符,但是 ||&& 运算符不是't,当您将它们用作二元运算符时,不会产生任何歧义。

As your previoes question about these operators has been about C#, I assume that this one is too.

Generally you want to use the short-circuit version of the conditional operators to avoid unneccesary operations. If the value of the first operand is enough to determine the result, the second operand needn't be evaluated.

When a condition relies on the previos condition being true, only the short-circuit operators work, for example doing a null check and property comparison:

if (myObj != null && myObj.State == "active")

Using the & operator in that case would not keep the second operand from being evaluated, and it would cause a null reference exception.

The non-shortcircuit operators are useful when you want both operands to always be evaluated, for example when they have a side effect:

if (DoSomeWork() & DoOtherWork())

Using the && operator would prevent the second method to be called if the first returned false.

The & and | are also binary operators, but as the || and && operators aren't, there is no ambiguity when you use them as binary operators.

爱的那么颓废 2024-10-27 06:30:33

这是一个非常普遍的问题,我假设您正在使用 Java、C# 或其他类似的语法进行讨论。在 VB 中,它相当于字符串上的 +,但我认为这是另一个故事了。

据我所知,如果您用 C# 语言进行讨论,您的说法是正确的。

Very general question and I'm assuming you're talking in Java, C#, or another similar syntax. In VB it's the equivalent of + on strings, but that's another story I assume.

As far as I know, your statement is correct if you're talking in terms of C#.

沉睡月亮 2024-10-27 06:30:33

如果它是Javascript,请查看这个答案: 使用 & ;& 作为 if 语句的短路?

那里也有关于 C# 用法的简短讨论。

Java 还有一些运算符,例如 |= : “|=”是什么意思在Java中是什么意思?

If it's Javascript then please look at this answer: Using &&'s short-circuiting as an if statement?

There is a short discussion on C# uses there too.

Java has a few more operators, such as |= : What does "|=" mean in Java?

寂寞笑我太脆弱 2024-10-27 06:30:33

C 使用 &作为任何数据类型上的一元运算符来获取数据的地址,

例如:

int i = 5;
cout<<&i;//print the address of i

某些语言允许您覆盖此类运算符以使它们执行您想要的任何操作!

C uses & as a unary operator on any data types to get the address of the data

for example:

int i = 5;
cout<<&i;//print the address of i

Some languages allow you to override such operators to make them do anything you want!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文