Search code examples
c++armadillo

Accessing intermediate armadillo object member without naming it [C++]


Using the armadillo library, one may write

// Log if this is non-finite
if ( ! averages.is_finite() )
{
    arma::uvec nonfinites = arma::find_nonfinite( averages );
    LOG_WARN( "There are ",  nonfinites.n_elem, " of ", averages.n_elem,
            " non-finite elements in the band border averages of the input signal." );
}

I'd like to simplify this without using the nonfinites intermediate vector, like so:

    LOG_WARN( "There are ",  (arma::find_nonfinite( averages )).n_elem, " of ", averages.n_elem,
            " non-finite elements in the band border averages of the input signal." );

But I get a g++ error message

/path/to/file.cpp:901:71: error: ‘arma::enable_if2<true, const arma::mtOp<long long un
signed int, arma::Mat<double>, arma::op_find_nonfinite> >::result’ has no member named ‘n_elem’
         LOG_WARN( "There are ", (arma::find_nonfinite( averages )).n_elem, " of ", averages.n_elem,

I'm unsure if I'm dealing with a general C++ issue or if the arma library function return type is not an arma::uvec (as indicated by the g++ error). which actually has the member n_elem. Is it required to convert the returned object into an uvec first?

Many thanks in advance for any explanation!


Solution

  • Use the .eval() member function to forcefully evaluate the expression:

    (arma::find_nonfinite( averages )).eval().n_elem
    

    Alternatively, directly convert the expression to uvec:

    arma::uvec(arma::find_nonfinite( averages )).n_elem