为什么 C++ 没有幂运算符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14626960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why doesn't C++ have a power operator?
提问by James Kanze
Many languages have a power operator; why doesn't C++? For example, Fortran and Python use **and is commonly written (in LaTeX, for example) using ^.
许多语言都有一个幂运算符;为什么没有 C++?例如,Fortran 和 Python 使用**并且通常使用^.
回答by James Kanze
C++ does have a power operator—it's written pow(x, y).
C++ 确实有一个幂运算符——它被写成pow(x, y).
Originally, C was designed with system software in mind, and
there wasn't much need for a power operator. (But it has
bitwise operators, like &and |, which are absent in a lot
of other languages.) There was some discussion of adding one
during standardization of C++, but the final consensus was more
or less:
最初,C 的设计考虑了系统软件,并没有太多需要电源运算符。(但它有按位运算符,如&and |,在很多其他语言中都没有。)在 C++ 的标准化过程中曾有一些关于添加一个的讨论,但最终的共识或多或少是:
It couldn't be
^, because the priority was wrong (and of course, having2. ^ 8 == 256., but2 ^ 8 == 10isn't very pleasant either).It couldn't be
**, because that would break existing programs (which might have something likex**p, withxanint, andpanint*).It could be
*^, because this sequence isn't currently legal in C or C++. But this would still require introducing an additional level of precedence.C and C++ already had enough special tokens and levels of precedence, and after discussions with the numerics community, it was concluded that there really wasn't anything wrong with
pow(x, y).
不可能是
^,因为优先级是错误的(当然,有2. ^ 8 == 256.,但2 ^ 8 == 10也不是很愉快)。它不可能是
**,因为这会破坏现有的程序(可能有这样的事情x**p,有x一个int和p一个int*)。可能是
*^,因为这个序列目前在 C 或 C++ 中是不合法的。但这仍然需要引入额外的优先级。C 和 C++ 已经有足够的特殊标记和优先级,在与数字社区讨论后,得出的结论是
pow(x, y).
So C++ left things as they were, and this doesn't seem to have caused any problems.
所以 C++ 保持原样,这似乎没有引起任何问题。
回答by Shai
For two reasons
有两个原因
The symbol
^is reserved for bit-wise xor operationYou may use
std::powto achieve the same functionality.
该符号
^保留用于按位异或运算您可以使用它
std::pow来实现相同的功能。
The nice thing about C++ is that you can overload the operatorto do whatever you like it to do!
C++ 的好处是你可以重载operator它来做你喜欢做的任何事情!
template< typename T >
T operator^( T x, T y ) {
return std::pow( x, y );
}
Howevertake into account that when you do that, other people who know C++and don't know you (and I believe there are quite a few of those) might have significantproblems understanding your code!
但是请注意,当您这样做时,其他认识C++和不认识您的人(我相信其中有很多人)可能会在理解您的代码时遇到重大问题!
回答by distantTransformer
You could help yourself if you want
如果你愿意,你可以帮助自己
struct DoubleMock
{
DoubleMock(double v){_v = v;}
double _v;
};
double operator^(DoubleMock x, DoubleMock y)
{
return pow(x._v,y._v);
}
double v = DoubleMock(2.0) ^ 2.0;
回答by Mats Petersson
Because that's the exclusive or bitwise operator.
因为那是异或按位运算符。
There are functions called "pow" that do what you want though.
有一些名为“pow”的函数可以做你想做的事。

