为什么重载的运算符不能被定义为类的静态成员? [英] Why overloaded operators cannot be defined as static members of a class?

查看:169
本文介绍了为什么重载的运算符不能被定义为类的静态成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++语法允许在struct /类内部定义重载运算符,如:

C++ syntax allows defining overloaded operators either inside the struct/class like:

struct X
{
   void operator+(X);
}

或外部的struct /类如下:

or outside of the struct/class like:

void operator+(X, X);

但不是

struct X
{
   static void operator+(X, X);
}

任何机构是否知道此决定的原因?为什么不允许第三种形式? (MSVC给出语法错误)。也许这背后有一个故事吗?

Does any body know reasons for this decision? Why the third form is not allowed? (MSVC gives a syntax error). Maybe there is some story behind this?

p.s。同时存在第一和第二定义造成模糊性:

p.s. Presence of the first and the second definitions at the same time creates ambiguity:

1>CppTest1.cxx
1>c:\ballerup\misc\cf_html\cpptest1.cxx(39) : error C2593: 'operator +' is ambiguous
1>        c:\ballerup\misc\cf_html\cpptest1.cxx(13): could be 'void B1::operator +(B1 &)'
1>        c:\ballerup\misc\cf_html\cpptest1.cxx(16): or       'void operator +(B1 &,B1 &)'
1>        while trying to match the argument list '(B1, B1)'

我不明白为什么这种歧义

I do not understand why this ambiguity is any better than between 1,3 or 2,3.

推荐答案

我对这个概念的任何C ++讨论没有特定的知识,所以请随意忽略这一点。

I have no specific knowledge of any C++ discussion of this concept, so feel free to ignore this.

但对我来说,你有问题的反向。问题应该是,为什么会允许这种语法?

But to me, you've got the question backwards. The question should be, "why would this syntax be allowed?"

它对当前语法没有任何优势。非静态成员函数版本具有与您建议的静态版本相同的私有成员访问权限。所以如果你需要访问的privates实现它,只是使它是一个非静态成员,正如你通常对一个类的大多数成员。

It provides no advantages at all over the current syntax. The non-static member function version has the same access to private members as your proposed static version. So if you need to access the privates to implement it, just make it a non-static member, exactly as you generally do with most members of a class.

它不会使得更容易实现非对称运算符(即: operator +(const X& x,const Y& y))。如果你需要私人访问来实现这一点,你仍然需要一个朋友声明他们在一个类。

It doesn't make it easier to implement asymmetric operators (ie: operator+(const X &x, const Y &y)). If you need private access to implement this, you'd still need a friend declaration for them in one of the classes.

所以我会说, t存在是它不是必需的。在非成员函数和非静态成员之间,包括所有必要的用例。

So I would say that the reason it doesn't exist is that it isn't necessary. Between non-member functions and non-static members, all of the necessary use cases are covered.

它以另一种方式:

自由函数可以做静态函数系统可以做的一切,和更多

Free functions can do everything that the static function system can, and more.

通过使用自由函数,您可以获得模板中使用的运算符的参数相关查找。你不能使用静态函数,因为那些必须是特定类的成员。而且您无法从类外部向类添加,而您可以添加到命名空间。所以如果你需要在一个特定的命名空间中放置一个操作符,以使一些ADL代码可以工作,你可以。你不能使用静态函数运算符。

Through the use of free functions, you can get argument-dependent lookup happening for operators used in templates. You can't do that with static functions, because those would have to be a member of a particular class. And you cannot add to a class from outside of the class, while you can add to a namespace. So if you need to put an operator in a particular namespace in order to make some ADL code work, you can. You can't do that with static function operators.

因此,自由函数是你提出的静态函数系统提供的一切的超集。由于没有允许它的好处,因此没有允许它的原因,因此是不允许的。

Thus, free functions are a superset of everything that your proposed static function system would provide. Since there is no benefit to allowing it, there is no reason to allow it, and therefore it is not allowed.


这样可以使用函数而不实例化它们?

which would make possible to use functors without instantiating them?

一个矛盾的术语。 函子是函数对象。类型是不是对象;因此,它不能是一个函子。它可以是一个类型,当实例化时,将导致一个函子。

That is a contradiction in terms. A "functor" is a "function object". A type is not an object; therefore, it cannot be a functor. It can be a type that, when instantiated, will result in a functor. But the type alone will not be a functor.

此外,能够声明 Typename :: operator() static不会意味着 Typename()会做你想要的。该语法已经具有实际意义:通过调用默认构造函数实例化 Typename

Furthermore, being able to declare Typename::operator() static would not mean that Typename() would do what you want. That syntax already has an actual meaning: instantiate a Typename temporary by calling the default constructor.

最后,所有这一切都不是 ,实际上有什么好处?大多数采用某种类型的可调用的模板函数与函数指针一样工作。为什么要限制您的界面,不仅仅是函数,而是要限制内部数据的函数? 这意味着你不能通过捕获lambdas等等。

Lastly, even if all that weren't the case, what good would that actually be? Most template functions that take a callable of some type work just as well with a function pointer as with a functor. Why would you want to restrict your interface, not merely to just functors, but to functors which cannot have internal data? That means you wouldn't be able to pass capturing lambdas and so forth.

一个不能包含状态的函子有什么好处?你为什么要强迫用户通过没有状态的函子?为什么要阻止用户使用lambdas?

What good is a functor that cannot possibly contain state? Why do you want to force the user into passing "functors" that don't have state? And why do you want to prevent the user from being able to use lambdas?

所以,你的问题来源于一个假设:即使我们有了它, t给你你想要的。

So your question is derived from a false assumption: even if we had it, it wouldn't give you what you want.

这篇关于为什么重载的运算符不能被定义为类的静态成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆