gpt4 book ai didi

C++ 17在两侧构建具有变体的运算符的最佳方法?

转载 作者:搜寻专家 更新时间:2023-10-31 00:53:39 25 4
gpt4 key购买 nike

我有一个项目对可变长度类型使用 std::variant :

using Length = std::variant<int, long, float, double, Fraction>;

Fraction 类已经重载了大部分运算符。

我想创建像这样的算术运算符:

Length operator+ (Length lhs, Length rhs);
Length operator- (Length lhs, Length rhs);
Length operator* (Length lhs, Length rhs);
Length operator/ (Length lhs, Length rhs);

但是比较运算符也是:

Length operator== (Length lhs, Length rhs);
Length operator!= (Length lhs, Length rhs);

Length operator> (Length lhs, Length rhs);
Length operator>= (Length lhs, Length rhs);
Length operator< (Length lhs, Length rhs);
Length operator<= (Length lhs, Length rhs);

这是我使用 std::visit 方法完成工作的模式。

Length operator* (Length lhs, Length rhs)
{
Length res;

std::visit([&res, rhs](auto& left)
{
using T = std::remove_cv_t<std::remove_reference_t<decltype(left)>>;

if constexpr (std::is_same_v<T, int>)
{
std::visit([&res, left](auto& right)
{
using T = std::remove_cv_t<std::remove_reference_t<decltype(right)>>;

if constexpr (std::is_same_v<T, int>) { res = left * right; } else
if constexpr (std::is_same_v<T, long>) { res = left * right; } else
if constexpr (std::is_same_v<T, float>) { res = left * right; } else
if constexpr (std::is_same_v<T, double>) { res = left * right; } else
if constexpr (std::is_same_v<T, Fraction>) { res = left * right; }
},
rhs);
}
else
if constexpr (std::is_same_v<T, long>)
{
std::visit([&res, left](auto& right)
{
using T = std::remove_cv_t<std::remove_reference_t<decltype(right)>>;

if constexpr (std::is_same_v<T, int>) { res = left * right; } else
if constexpr (std::is_same_v<T, long>) { res = left * right; } else
if constexpr (std::is_same_v<T, float>) { res = left * right; } else
if constexpr (std::is_same_v<T, double>) { res = left * right; } else
if constexpr (std::is_same_v<T, Fraction>) { res = left * right; }
},
rhs);
}
else
if constexpr (std::is_same_v<T, float>)
{
std::visit([&res, left](auto& right)
{
using T = std::remove_cv_t<std::remove_reference_t<decltype(right)>>;

if constexpr (std::is_same_v<T, int>) { res = left * right; } else
if constexpr (std::is_same_v<T, long>) { res = left * right; } else
if constexpr (std::is_same_v<T, float>) { res = left * right; } else
if constexpr (std::is_same_v<T, double>) { res = left * right; } else
if constexpr (std::is_same_v<T, Fraction>) { res = left * right; }
},
rhs);
}
else
if constexpr (std::is_same_v<T, double>)
{
std::visit([&res, left](auto& right)
{
using T = std::remove_cv_t<std::remove_reference_t<decltype(right)>>;

if constexpr (std::is_same_v<T, int>) { res = left * right; } else
if constexpr (std::is_same_v<T, long>) { res = left * right; } else
if constexpr (std::is_same_v<T, float>) { res = left * right; } else
if constexpr (std::is_same_v<T, double>) { res = left * right; } else
if constexpr (std::is_same_v<T, Fraction>) { res = left * right; }
},
rhs);
}
else
if constexpr (std::is_same_v<T, Fraction>)
{
std::visit([&res, left](auto& right)
{
using T = std::remove_cv_t<std::remove_reference_t<decltype(right)>>;

if constexpr (std::is_same_v<T, int>) { res = left * right; } else
if constexpr (std::is_same_v<T, long>) { res = left * right; } else
if constexpr (std::is_same_v<T, float>) { res = left * right; } else
if constexpr (std::is_same_v<T, double>) { res = left * right; } else
if constexpr (std::is_same_v<T, Fraction>) { res = left * right; }
},
rhs);
}
},
lhs);

return res;
}

这行得通,但这是多余的、不美观的,而且进行大型操作过程可能不会那么快。

最佳答案

因为你的操作数是 variant 类型,所以就是 visit他们一起:

Length operator* (Length lhs, Length rhs)
{
return std::visit([](auto const &l, auto const &r) -> Length {
return l * r;
}, lhs, rhs);
}

标准库已经为您执行了所需的逻辑。

关于C++ 17在两侧构建具有变体的运算符的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48058140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com