NICTA / SmartGridToolbox

Smart Grid Simulation Library (C++14)
http://nicta.github.io/SmartGridToolbox
24 stars 10 forks source link

Use of auto in armadillo #15

Closed dexterurbane closed 8 years ago

dexterurbane commented 9 years ago

The following doesn't work under g++4.9, but does work under clang:

#include <iostream>
#include <armadillo>

int main()
{
        arma::Col<std::complex<double>> v = {0.0, 0.0, 0.0, 0.0};
        auto v1 = v(arma::span(1, 2));
        auto v2 = arma::real(v1);
        auto v3 = arma::real(v(arma::span(1, 2)));

        std::cout << v << std::endl;
        std::cout << v1 << std::endl;
        std::cout << v2 << std::endl;
        std::cout << v3 << std::endl;
}

The final cout gives an empty vector.

Conrad Sanderson looked at this code and advised to avoid using "auto" with armadillo, as one might end up referencing a temporary object that no longer exists. Thus, it might be a good idea to audit the code and reduce potential for future problems.

dexterurbane commented 9 years ago

Further: a good win-win way to go would be to do in place operations e.g. using +=, *= etc.

dexterurbane commented 8 years ago

Closing, as I think we're pretty stable wrt this issue.