Better decltype for Polynomial::add return type

This commit is contained in:
trotFunky 2019-05-14 12:43:53 +02:00
parent f51d403b8e
commit 004142f85a

View file

@ -35,7 +35,7 @@ public:
bool equals(const Polynomial<T1>&) const; bool equals(const Polynomial<T1>&) const;
template <typename T1> template <typename T1>
auto add(const Polynomial<T1>& operand) const -> Polynomial<decltype(static_cast<T>(0) + operand[0])>; auto add(const Polynomial<T1>& operand) const -> Polynomial<decltype(T{} + T1{})>;
template <typename T1> template <typename T1>
friend std::ostream& operator<<(std::ostream&, const Polynomial<T1>&); friend std::ostream& operator<<(std::ostream&, const Polynomial<T1>&);
@ -62,6 +62,7 @@ private:
template<typename T> template<typename T>
Polynomial<T>::Polynomial() Polynomial<T>::Polynomial()
{ {
// Not bad, but could be better with C++20. Not that useful either
static_assert(std::is_arithmetic<T>::value,"Polynomial must be of an arithmetic type!"); static_assert(std::is_arithmetic<T>::value,"Polynomial must be of an arithmetic type!");
factors = {0}; factors = {0};
} }
@ -197,7 +198,7 @@ bool Polynomial<T>::equals(const Polynomial<T1>& operand) const
template<typename T> template<typename T>
template<typename T1> template<typename T1>
auto Polynomial<T>::add(const Polynomial<T1>& operand) const -> Polynomial<decltype(static_cast<T>(0) + operand[0])> auto Polynomial<T>::add(const Polynomial<T1>& operand) const -> Polynomial<decltype(T{} + T1{})>
{ {
bool isLargest = true; bool isLargest = true;
@ -212,7 +213,7 @@ auto Polynomial<T>::add(const Polynomial<T1>& operand) const -> Polynomial<declt
largestSize = operand.factors.size(); largestSize = operand.factors.size();
} }
std::vector<decltype(static_cast<T>(0)+operand[0])> resultPolynomial = {}; std::vector<decltype(T{} + T1{})> resultPolynomial = {};
resultPolynomial.reserve(largestSize); resultPolynomial.reserve(largestSize);
for(int i = 0;i<smallestSize;i++) for(int i = 0;i<smallestSize;i++)
@ -225,7 +226,7 @@ auto Polynomial<T>::add(const Polynomial<T1>& operand) const -> Polynomial<declt
resultPolynomial.push_back((isLargest ? factors[i] : operand[i])); resultPolynomial.push_back((isLargest ? factors[i] : operand[i]));
} }
return Polynomial<decltype(static_cast<T>(0)+operand[0])>(resultPolynomial); return Polynomial<decltype(T{} + T1{})>(resultPolynomial);
} }
//////////////////// ////////////////////