Better decltype for Polynomial::add return type
This commit is contained in:
parent
f51d403b8e
commit
004142f85a
1 changed files with 5 additions and 4 deletions
|
@ -35,7 +35,7 @@ public:
|
|||
bool equals(const Polynomial<T1>&) const;
|
||||
|
||||
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>
|
||||
friend std::ostream& operator<<(std::ostream&, const Polynomial<T1>&);
|
||||
|
@ -62,6 +62,7 @@ private:
|
|||
template<typename T>
|
||||
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!");
|
||||
factors = {0};
|
||||
}
|
||||
|
@ -197,7 +198,7 @@ bool Polynomial<T>::equals(const Polynomial<T1>& operand) const
|
|||
|
||||
template<typename T>
|
||||
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;
|
||||
|
||||
|
@ -212,7 +213,7 @@ auto Polynomial<T>::add(const Polynomial<T1>& operand) const -> Polynomial<declt
|
|||
largestSize = operand.factors.size();
|
||||
}
|
||||
|
||||
std::vector<decltype(static_cast<T>(0)+operand[0])> resultPolynomial = {};
|
||||
std::vector<decltype(T{} + T1{})> resultPolynomial = {};
|
||||
resultPolynomial.reserve(largestSize);
|
||||
|
||||
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]));
|
||||
}
|
||||
|
||||
return Polynomial<decltype(static_cast<T>(0)+operand[0])>(resultPolynomial);
|
||||
return Polynomial<decltype(T{} + T1{})>(resultPolynomial);
|
||||
}
|
||||
|
||||
////////////////////
|
||||
|
|
Loading…
Add table
Reference in a new issue