创建一个接受 2 个输入参数的文字运算符函数
Create a literal operator function that takes 2 input parameters
我有一个定义类型 Coordinates
像这样:
#include <array>
using Coordinates = std::array<double, 3>;
为此我调用了以下运算符重载函数:
Coordinates operator+(const Coordinates& lhs, const Coordinates& rhs);
Coordinates operator*(const Coordinates& lhs, const Coordinates& rhs);
两个重载都有效,所以如果我有 2 个 Coordinates
变量:
C1 = { 1., 2., 3.}
和
C2 = { 1., 2., 3. }
C1+C2
returns { 2., 4., 6.}
C1*C2
returns { 1., 4., 9.}
现在我想定义一个 *+
运算符,这样:
C1*+C2
returns 1. + 4. + 9.
或 14.
我尝试了以下实现:
Coordinates operator*+(const Coordinates& lhs, const Coordinates& rhs)
{
return lhs[0] * rhs[0] + lhs[1] * rhs[1] + lhs[2] * rhs[2];
}
但是,*+
不是预定义的运算符。然后我尝试了这种格式:
Coordinates operator "" *+(const Coordinates& lhs, const Coordinates& rhs)
{
return lhs[0] * rhs[0] + lhs[1] * rhs[1] + lhs[2] * rhs[2];
}
但我明白了:invalid literal operator name
。可以理解这个怎么样:
double operator "" _d_(const Coordinates& lhs, const Coordinates& rhs)
{
return lhs[0] * rhs[0] + lhs[1] * rhs[1] + lhs[2] * rhs[2];
}
_d_
代表点积中的点,但现在我收到此错误 too many parameters for this literal
。是否可以为点积定义一个运算符,或者我是否必须编写一个 dot()
函数?
您只能为您的类型重载 38 个现有运算符。此处列出:https://en.cppreference.com/w/cpp/language/operators
op
- any of the following 38 (until C++20) 39 (since C++20)
operators:
+
-
*
/
%
^
&
|
~
!
=
<
>
+=
-=
*=
/=
%=
^=
&=
|=
<<
>>
>>=
<<=
==
!=
<=
>=
<=>
(since C++20) &&
||
++
--
,
->*
->
(
)
[
]
文字运算符处理单个参数并将文字(如 42、"foobar")转换为对象。由于您已经拥有对象,因此您必须使用运算符重载。选择任何可用的。
首先,您可以查看 list of operators 以查看此处存在的内容以及哪些可以重载,哪些不能重载。
关于第二点,您可以尝试使用 this answer 之类的方法来制作自定义运算符。请记住,这在技术上并不是一个新的运算符(因为您不能在 C++ 中创建自己的运算符),但确实涉及一些模板魔术来完成同样的事情。
我有一个定义类型 Coordinates
像这样:
#include <array>
using Coordinates = std::array<double, 3>;
为此我调用了以下运算符重载函数:
Coordinates operator+(const Coordinates& lhs, const Coordinates& rhs);
Coordinates operator*(const Coordinates& lhs, const Coordinates& rhs);
两个重载都有效,所以如果我有 2 个 Coordinates
变量:
C1 = { 1., 2., 3.}
和
C2 = { 1., 2., 3. }
C1+C2
returns { 2., 4., 6.}
C1*C2
returns { 1., 4., 9.}
现在我想定义一个 *+
运算符,这样:
C1*+C2
returns 1. + 4. + 9.
或 14.
我尝试了以下实现:
Coordinates operator*+(const Coordinates& lhs, const Coordinates& rhs)
{
return lhs[0] * rhs[0] + lhs[1] * rhs[1] + lhs[2] * rhs[2];
}
但是,*+
不是预定义的运算符。然后我尝试了这种格式:
Coordinates operator "" *+(const Coordinates& lhs, const Coordinates& rhs)
{
return lhs[0] * rhs[0] + lhs[1] * rhs[1] + lhs[2] * rhs[2];
}
但我明白了:invalid literal operator name
。可以理解这个怎么样:
double operator "" _d_(const Coordinates& lhs, const Coordinates& rhs)
{
return lhs[0] * rhs[0] + lhs[1] * rhs[1] + lhs[2] * rhs[2];
}
_d_
代表点积中的点,但现在我收到此错误 too many parameters for this literal
。是否可以为点积定义一个运算符,或者我是否必须编写一个 dot()
函数?
您只能为您的类型重载 38 个现有运算符。此处列出:https://en.cppreference.com/w/cpp/language/operators
op
- any of the following 38 (until C++20) 39 (since C++20) operators:
+
-
*
/
%
^
&
|
~
!
=
<
>
+=
-=
*=
/=
%=
^=
&=
|=
<<
>>
>>=
<<=
==
!=
<=
>=
<=>
(since C++20)&&
||
++
--
,
->*
->
(
)
[
]
文字运算符处理单个参数并将文字(如 42、"foobar")转换为对象。由于您已经拥有对象,因此您必须使用运算符重载。选择任何可用的。
首先,您可以查看 list of operators 以查看此处存在的内容以及哪些可以重载,哪些不能重载。
关于第二点,您可以尝试使用 this answer 之类的方法来制作自定义运算符。请记住,这在技术上并不是一个新的运算符(因为您不能在 C++ 中创建自己的运算符),但确实涉及一些模板魔术来完成同样的事情。