Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does C++ define the overloaded operator in the namespace of the Operand

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how C++ defines overloaded operators in the namespace of operands". The explanation in this article is simple, clear and easy to learn and understand. let's study and learn how C++ defines overloaded operators in the namespace of operands.

C.168: define the overloaded operator in the namespace of the Operand

Reason (reason)

Readability. Provides the ability to use ADL discovery operators. Avoid inconsistencies in different namespaces.

Example (sample)

Struct S {}

Bool operator== (S, S); / / OK: in the same namespace as S, and even next to S

S s

Bool x = (s = s)

This is what the default equality comparison operator does, if there is such a default.

Example (sample)

Namespace N {

Struct S {}

Bool operator== (S, S); / / OK: in the same namespace as S, and even next to S

}

N::S s

Bool x = (s = = s); / / finds by ADL

Example, bad (negative example) struct S {}

S s

Namespace N {

S::operator! (sa) {return true;}

S not_s =! s

}

Namespace M {

S::operator! (sa) {return false;}

S not_s =! s

}

The meaning of! s in the N and M namespaces in the code is different. It's going to be very confusing. Removing the definition of namespace M will increase the likelihood of errors.

Note (Note)

If you define binocular operators for two different types in different namespaces, you cannot follow this guideline. For example:

Vec::Vector operator* (const Vec::Vector&, const Mat::Matrix&)

This is probably at its best.

See also (reference)

This can be said to be a special case of the rule that the helper should be defined in a namespace with the classes it helps.

Enforcement (implementation recommendations)

The tag does not have an operator defined in the same namespace as the Operand.

Thank you for your reading, the above is the content of "how C++ defines overloaded operators in the namespace of operands". After the study of this article, I believe you have a deeper understanding of how C++ defines overloaded operators in the namespace of operands, and the specific use still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report