<分区>
虽然我正在编写一些代码作为语法糖,例如 python 和其他语言中已知的幂运算符的实现,运算符定义是可以的,但是操作数与运算符签名匹配的表达式会产生错误,如运营商从未被定义。有没有办法(编译器选项)为内置类型实现新的运算符?
#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
};
我正在使用 mingw。
编辑:clang 甚至不支持运算符的定义。