在 C++ 中显式调用原始运算符函数

发布于 2024-09-29 18:23:29 字数 550 浏览 3 评论 0原文

int a, b, c; 

//do stuff. For e.g., cin >> b >> c; 

c = a + b;          //works 
c = operator+(a,b); //fails to compile, 'operator+' not defined. 

另一方面,这有效 -

class Foo
{
 int x; 
public:
 Foo(int x):x(x) {} 

 Foo friend operator+(const Foo& f, const Foo& g)
 {
  return Foo(f.x + g.x); 
 }

};    

Foo l(5), m(10); 

Foo n = operator+(l,m); //compiles ok! 
  • 是否可以直接调用基本类型(如 int)的运算符+(和其他运算符)?
  • 如果是,怎么办?
  • 如果不是,是否有 C++ 参考用语明确表明这是不可行的?
int a, b, c; 

//do stuff. For e.g., cin >> b >> c; 

c = a + b;          //works 
c = operator+(a,b); //fails to compile, 'operator+' not defined. 

This on the other hand works -

class Foo
{
 int x; 
public:
 Foo(int x):x(x) {} 

 Foo friend operator+(const Foo& f, const Foo& g)
 {
  return Foo(f.x + g.x); 
 }

};    

Foo l(5), m(10); 

Foo n = operator+(l,m); //compiles ok! 
  • Is it even possible to invoke operator+ (and other operators) of primitive types (like int) directly?
  • If yes, how?
  • If not, is there a C++ reference verbiage that makes it clear that this is not doable?

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

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

发布评论

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

评论(2

流星番茄 2024-10-06 18:23:29

首先,将内置运算符作为函数调用是行不通的,因为语言规范从未说过此类函数存在。内置运算符只是运算符。它们背后没有实现函数,只是因为语言规范从未表明它们的存在。基于函数的实现仅特定于重载运算符。

其次,在重载解析期间,内置运算符确实由其虚构的类函数对应项表示,但禁止“显式”类函数调用内置运算符的措辞出现在 13.6/1 中

候选运算符的功能是
代表内置运算符
第 5 条中的定义在
本款。这些候选人
函数参与运算符
重载解析过程为
13.3.1.2 中描述并使用
没有其他目的。

Firstly, invoking built-in operators as functions will not work simply because the language specification never says that such functions exist. Built-in operators are just operators. There are no implementing functions behind them simply because the language specification never suggests their existence. Function-based implementations are specific to overloaded operators only.

Secondly, during overload resolution the built-in operators are indeed represented by their imaginary function-like counterparts, but the wording that prohibits "explicit" function-like invocation of built-in operators is present in 13.6/1

The candidate operator functions that
represent the built-in operators
defined in clause 5 are specified in
this subclause. These candidate
functions participate in the operator
overload resolution process as
described in 13.3.1.2 and are used
for no other purpose
.

千寻… 2024-10-06 18:23:29

来自 http://www.parashift.com/c++-faq-lite/ inside-types.html

我可以定义一个与内置/内在/原始类型一起使用的运算符重载吗?

不,C++ 语言要求
你的操作符重载至少需要
“类类型”的一个操作数或
枚举类型。 C++语言
不会让你定义一个运算符
所有的操作数/参数都是
原始类型。

例如,您不能定义
需要两个 char* 的运算符 == 和
使用字符串比较。那挺好的
新闻,因为如果 s1 和 s2 是类型
char*,表达式 s1 == s2 已经
有明确的含义:它
比较两个指针,而不是两个
这些指针指向的字符串。
无论如何你不应该使用指针。使用
std::string 而不是 char*。

如果C++让你重新定义意义
内置类型上的运算符,您
永远不会知道 1 + 1 是什么:它
取决于获得的标头
包括以及是否其中之一
标题重新定义除了意思,
例如,减法。

C++ 标准§13.5.6

运算符函数应当是非静态成员函数或非成员函数,并且至少有一个类型为类、类引用、枚举或枚举引用的参数。无法更改运算符的优先级、分组或操作数数量。为每种类型预定义的运算符 =、(一元)& 和 ,(逗号)的含义可以通过定义实现这些运算符的运算符函数来针对特定类和枚举类型进行更改。运算符函数的继承方式与其他基类函数相同。

From http://www.parashift.com/c++-faq-lite/intrinsic-types.html

Can I define an operator overload that works with built-in / intrinsic / primitive types?

No, the C++ language requires that
your operator overloads take at least
one operand of a "class type" or
enumeration type. The C++ language
will not let you define an operator
all of whose operands / parameters are
of primitive types.

For example, you can't define an
operator== that takes two char*s and
uses string comparison. That's good
news because if s1 and s2 are of type
char*, the expression s1 == s2 already
has a well defined meaning: it
compares the two pointers, not the two
strings pointed to by those pointers.
You shouldn't use pointers anyway. Use
std::string instead of char*.

If C++ let you redefine the meaning of
operators on built-in types, you
wouldn't ever know what 1 + 1 is: it
would depend on which headers got
included and whether one of those
headers redefined addition to mean,
for example, subtraction.

C++ Standard §13.5.6

An operator function shall either be a non-static member function or be a non-member function and have at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration. It is not possible to change the precedence, grouping, or number of operands of operators. The meaning of the operators =, (unary) &, and , (comma), predefined for each type, can be changed for specific class and enumeration types by defining operator functions that implement these operators. Operator functions are inherited in the same manner as other base class functions.

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