C++ 内置类型的重载运算符
虽然我编写了一些语法糖代码,比如python和其他语言中已知的幂运算符的这个实现,但运算符定义是可以的,但是操作数与运算符签名匹配的表达式会产生错误,因为从未定义运算符。是否有一种方法(编译器选项)来实现内置类型的新运算符C++ 内置类型的重载运算符,c++,operator-overloading,C++,Operator Overloading,虽然我编写了一些语法糖代码,比如python和其他语言中已知的幂运算符的这个实现,但运算符定义是可以的,但是操作数与运算符签名匹配的表达式会产生错误,因为从未定义运算符。是否有一种方法(编译器选项)来实现内置类型的新运算符 #include <iostream> #include <cmath> template<typename t_Float> struct PowerTmp { t_Float value; }; PowerTmp&l
#include <iostream>
#include <cmath>
template<typename t_Float>
struct PowerTmp {
t_Float value;
};
PowerTmp<double> operator*(double f) {
return {f};
};
double operator*(double l, PowerTmp<double> r) {
return std::pow(l, r.value);
};
int main() {
std::cout << 10.5 *PowerTmp<double>{2.0} << '\n';
cout << 10.5 ** 2.0 << '\n'; //error
};
#包括
#包括
模板
结构PowerTmp{
t_浮点数;
};
PowerTmp运算符*(双f){
返回{f};
};
双运算符*(双l,PowerTmp r){
返回标准::功率(l,r.值);
};
int main(){
std::cout目前您所要求的是不可能的。您不能重载内置的运算符。因此,您的第一个重载是非法的,因为您试图为double
定义一元运算符*
。不确定gcc为什么不抱怨
但是,您可以使用UDL“更改”文本的类型。以下是一个简化示例,用于演示:
struct Exponent { long double value; };
struct PowerDouble { long double value; };
Exponent operator""_exp(long double exponent) {
return{exponent};
}
PowerDouble operator*(Exponent f) {
return{f.value};
}
long double operator*(long double l, PowerDouble r) {
return std::pow(l, r.value);
}
long double operator*(long double l, Exponent r) {
return l * r.value;
}
然后您可以这样使用它:
std::cout << 10.5 ** 2._exp << '\n';
std::cout << 10.5 * 2._exp << '\n';
std::cout目前您所要求的是不可能的。您不能为内置函数重载运算符。因此,您的第一次重载是非法的,因为您试图为双精度定义一元运算符*
。不确定gcc为什么不抱怨
但是,您可以使用UDL“更改”文本的类型。以下是一个简化示例,用于演示:
struct Exponent { long double value; };
struct PowerDouble { long double value; };
Exponent operator""_exp(long double exponent) {
return{exponent};
}
PowerDouble operator*(Exponent f) {
return{f.value};
}
long double operator*(long double l, PowerDouble r) {
return std::pow(l, r.value);
}
long double operator*(long double l, Exponent r) {
return l * r.value;
}
然后您可以这样使用它:
std::cout << 10.5 ** 2._exp << '\n';
std::cout << 10.5 * 2._exp << '\n';
std::cout否,如果只有内置类型的参数,则不能重载运算符。即使该类型不存在该运算符
您可以做的是创建中间类型。例如:
struct EnhancedDouble {
double d;
};
struct PowPrecursor {
double d;
};
PowPrecursor operator*(EnhancedDouble b) {
return { b.d };
}
EnhancedDouble operator*(EnhancedDouble lhs, PowPrecursor rhs) {
return { std::pow(lhs.d, rhs.d) };
}
您甚至可以使用用户定义的文字来增加一点
EnhancedDouble operator""_ed(long double d) {
return { (double)d };
}
加入一个操作符否,如果只有内置类型的参数,则不能重载该操作符。即使该类型不存在该操作符
您可以做的是创建中间类型。例如:
struct EnhancedDouble {
double d;
};
struct PowPrecursor {
double d;
};
PowPrecursor operator*(EnhancedDouble b) {
return { b.d };
}
EnhancedDouble operator*(EnhancedDouble lhs, PowPrecursor rhs) {
return { std::pow(lhs.d, rhs.d) };
}
您甚至可以使用用户定义的文字来增加一点
EnhancedDouble operator""_ed(long double d) {
return { (double)d };
}
< >在<代码>操作器中,C++中没有<代码> **/COD>操作符,如果您期望这是Python中的幂运算,则应该使用<代码> STD::POW或自己编写function@CoryKramer但是,当使用用户定义的类型而不是double并将数字强制转换为该类型时,此处定义的幂运算符有效(实际上是两个运算符,*-前缀运算符和乘法运算符!)C++中没有<代码> **/COND>运算符,如果你希望它是Python中的幂运算,你应该使用<代码> STD::POW或自己编写function@CoryKramer但是,当使用用户定义的类型而不是double并将数字强制转换为该类型时,此处定义的幂运算符有效(实际上是两个运算符,*-前缀运算符和乘法运算符!)注意,10.5*2.\u exp
也将执行求幂运算。10.5****2.\u exp
@BenjaminLindley您是对的,修正:)感谢有了这个,10.5*2.\u exp
也将执行求幂运算。10.5*2.\u exp
@BenjaminLindley您是对的,修正:)谢谢