Search code examples
c++c++11initializer-listarmadillo

Armadillo initializer list is not working


I am using the MSVC2013 64bit compiler under Windows 10.

According to:

std::cout << arma::arma_version::as_string() << std::endl;

I have version 6.100.1 (Midnight Blue) of the Armadillio library.

I have C++11 enabled, for example

auto il = { 10, 20, 30 };
for(auto ele : il)
    cout<<ele<<endl;

is working. Also the library is correctly added, as the following code runs:

vec v;
v<<10<<20<<30;
cout<<v;

But trying to use initializer lists for Armadillio fails.

vec v = { 1.0, 2.0, 3.0 };

causes the compile error:

error: C2440: 'initializing' : cannot convert from 'initializer-list' to 'arma::Col' No constructor could take the source type, or constructor overload resolution was ambiguous


Solution

  • In the folder armadillo-6.100.1\include\armadillo_bits there is a config file called config.hpp.

    There you find a paragraph saying:

    #if !defined(ARMA_USE_CXX11)
    // #define ARMA_USE_CXX11
    //// Uncomment the above line to forcefully enable use of C++11 features (eg. initialiser lists).
    //// Note that ARMA_USE_CXX11 is automatically enabled when a C++11 compiler is detected.
    #endif
    

    So it looks like the MSVC2013 64bit is not detected as a C++11 compiler from Armadillio. So uncommenting the line

    // #define ARMA_USE_CXX11
    

    Solved my problem. Now this is working like charm:

    vec v = { 1.0, 2.0, 3.0 };
    cout<<v;