犰狳初始值设定项列表不起作用

Armadillo initializer list is not working

我在 Windows 10.

下使用 MSVC2013 64 位编译器

根据:

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

我有 Armadillio 库的 6.100.1 版(午夜蓝)。

我启用了 C++11,例如

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

正在工作。库也被正确添加,如下代码运行:

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

但是尝试使用犰狳的初始化列表失败。

vec v = { 1.0, 2.0, 3.0 };

导致编译错误:

错误:C2440:'initializing':无法从 'initializer-list' 转换为 'arma::Col' 没有构造函数可以采用源类型,或者构造函数重载解析不明确

文档说 vecCol<double 的类型定义:

For convenience the following typedefs have been defined:
vec = colvec = Col< double >

如果我们查看 Col 构造函数,我们会发现以下接受初始化列表的构造函数:

#if defined(ARMA_USE_CXX11)

  template<typename eT>
  inline
  Col<eT>::Col(const std::initializer_list<eT>& list)
  {
    <...>   
  }

所以我的猜测是 ARMA_USE_CXX11 未定义,因此无法访问此构造函数。

在文件夹 armadillo-6.100.1\include\armadillo_bits 有一个名为 config.hpp.

的配置文件

在那里你找到一段说:

#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

所以看起来 MSVC2013 64 位没有被检测为 Armadillio 的 C++11 编译器。所以取消注释行

// #define ARMA_USE_CXX11

解决了我的问题。现在这就像魅力一样工作:

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