Description
@sunethwarna found that the following code fails when using GCC:
~cpp
array_1d
const auto result = rNormal / norm_2(rNormal);
~
with Clang the type of result is correctly determined as array_1d<double, 3>
With GCC however the resulting type is const boost::numeric::ublas::vector_binary_scalar2<Kratos::array_1d<double, 3>, const double, boost::numeric::ublas::scalar_divides<double, double> > which gives problems later on
Scope
To Reproduce
use auto, see above
Expected behavior
Gcc should produce the same as clang
Environment
Tested on multiple machines with different versions of Clang and Gcc. Always works with Clang and always fails with Gcc
E.g.:
any idea why this is happening?
@KratosMultiphysics/technical-committee
Mm no idea to be honest, I will investigate
@philbucher @sunethwarna , which version of boost are you using?
I think 1.73
I am using 1.72, the default version in Manjaro.
Seems that you are creating an expression of division instead of the result array. So if the expression is used in another place after the original normal array is gone of scope. It will be a memory error and it will mysteriously crashed.
As a rule of thumb, you should avoid auto for the expressions unless you are sure about the life time of the terms involved in the expression.
Also performance is an issue here. Because if you use result 3 times, you are evaluating the division 3 times.
So in any case I would suggest to store the values in a array_1d instead of creating auto expression:
const array_1d<double,3> result = rNormal / norm_2(rNormal);
or directly
rNormal /= norm_2(rNormal);
FYI @armingeiser
Thanks for the explanation! I solved a similar problem like this:
auto new_array = rNormal;
new_array /= norm_2(rNormal);
Is there any chance such a not recommended use of auto for expressions is detected by the compiler?
Is there any chance such a not recommended use of auto for expressions is detected by the compiler?
as far as I understood there is not right @roigcarlo ?
Not an expert in the topic, but I would say no. I will try to learn more about it to see if I can find anything on this topic.
Shall we close this?