Multiplication of a scalar with `FieldVector<T,1>` does not result in a vector
When multiplying a FieldVector<K,1>
with a scalar S != K
the overload
//! Binary multiplication, when using FieldVector<K,1> like K
template<class K>
constexpr FieldVector<K,1> operator* (const K a, const FieldVector<K,1>& b) noexcept
{
return a*b[0];
}
is not selected, since the scalar must be identical with the field-type of the FieldVector
in that case. It still works, but in an unexpected way:
auto vec = FieldVector<double,1>{1.0};
auto expr = 2 * vec; // => decltype(expr) = double
// 1. calls FieldVector<double,1>::operator (double&)() => conversion of the vector in a double&
// 2. multiplication int * double& => double
auto expr2 = 2.0 * vec // => decltype(expr2) = FieldVector<double,1>
IMHO, this is not the expected behavior. A FieldVector<K,1>
is first-of-all a vector an multiplication with a scalar should result in a vector. The case that multiplication with a scalar other than the type K
leads to a different behavior seems strange to me.
See also the issue staging/dune-functions#88 (closed) where this behavior silently has hidden a bug in the code. The return type of an expression might be used to define other types. On this case it lead to the wrong definition of the derivative type of a function and thus to a wrong assignment of a FieldVector<double,1>
to a FieldMatrix<double,1>
. This assignment (conversion) was also possible (and should not), but due to another issue not discussed here.
With !1401 the issue is fixed, since some of the specializations for FieldVector<T,1>
are removed. It then uses the more general multiplication with a scalar type that just hast to fulfill IsNumber<F>
. There are a few other operations not scalar multiplication that still rely on the argument type beeing exactly the field_type
of the vector. I have not checked, but expect similar unexpected behavior there.