【问题标题】:C++ Armadillo: GCC vs VC++2013: Operator () and overloadingC++ Armadillo:GCC 与 VC++2013:运算符 () 和重载
【发布时间】:2014-03-24 16:55:30
【问题描述】:

我正在尝试使用 Armadillo C++ 库开发 Linux/Win64 应用程序。以下代码在 GCC-4.7 中编译,但在 Visual Studio 2013 中使用 Armadillo 提供的 VS 项目文件编译失败。

#include <iostream>
#include "armadillo"

using namespace arma;
using namespace std;

//works in GCC-4.7
//VC++2013: compile error: C3066
void foo1(vec::fixed<4> &bar)
{
    bar(1) = 1.;
}

//works
void foo2(vec::fixed<4> &bar)
{
    bar.at(2) = 1.;
}

//works
void foo3(vec &bar)
{
    bar(3) = 1.;
}

int main(int argc, char** argv)
{
    cout << "Armadillo version: " << arma_version::as_string() << endl;
    vec::fixed<4> bar;
    bar.zeros();
    foo1(bar);
    foo2(bar);
    foo3(bar);
    cout << "Bar: " << bar << endl;
    return 0;
}

函数foo1出现错误:

1>example1.cpp(11): error C3066: there are multiple ways that an object of this type can be called with these arguments
1>          ../armadillo_bits/Col_bones.hpp(186): could be 'const arma::subview_col<eT> arma::Col<eT>::operator ()(const arma::span &) const'
1>          with
1>          [
1>              eT=double
1>          ]
1>          ../armadillo_bits/Col_bones.hpp(186): or       'arma::subview_col<eT> arma::Col<eT>::operator ()(const arma::span &)'
1>          with
1>          [
1>              eT=double
1>          ]
1>          ../armadillo_bits/Col_bones.hpp(186): or       'double &arma::Mat<double>::operator ()(const arma::uword)'
1>          ../armadillo_bits/Col_bones.hpp(186): or       'const double &arma::Mat<double>::operator ()(const arma::uword) const'
1>          ../armadillo_bits/Col_bones.hpp(205): or       'double &arma::Col<double>::fixed<4>::operator ()(const arma::uword)'
1>          ../armadillo_bits/Col_bones.hpp(206): or       'const double &arma::Col<double>::fixed<4>::operator ()(const arma::uword) const'
1>          while trying to match the argument list '(int)'

显然我想要倒数第二个选择,其他的不应该基于类型推断应用。 GCC 似乎同意,所以 VC++ 如何解决这些重载的运算符一定有什么不同?有趣的是,如果我使用 foo2 中的 .at() 方法,事情就会解决。但是.at() 以几乎相同的方法模式重载,那么为什么会这样呢?我在实际代码中遇到了与 operator= 相关的问题,所以我怀疑这里的操作符有什么特别之处。有什么不难看的方法来解决这个问题吗?我想使用普通的operator() 而不是方法.at()

【问题讨论】:

  • 作为一个额外的数据点,Clang 是怎么说的?
  • 它在 Clang 3.4 下编译得很好。这看起来像是 MS VC++ 中的一个错误,因为第一个建议显然是虚假的 ("... 可能是 const arma::subview_col arma::Col::operator ()(const arma: :span &) 常量")。查看 Armadillo 源代码,整数参数 1 和 arma::span 之间没有隐式转换。请参见 include/armadillo_bits/span.hpp 中的第 58 行,其中span 类的相关构造函数标记为explicit。 MS VC++ 的其他建议也是虚假的,因为 Col::fixed 类继承自 Col 类并重新定义了 operator()
  • 这确实是一个 MSVC 错误,其中 explicit 被忽略;请参阅stackoverflow.com/questions/20498142/… 了解更多信息。

标签: c++ visual-c++ operator-overloading armadillo


【解决方案1】:

这与MSVC connect bug #811334 有关,根据他在此处评论中链接的SO post Ryan,应该在 MSVC 2015 中修复。

(错误是 MSVC 忽略了构造函数上的 explicit 关键字——它与链接的 SO 帖子的代码有关,但不完全相同,因为链接的 SO 帖子和报告处理 @987654324 的丢失@ 在转换运算符上。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-19
    • 2012-11-26
    • 1970-01-01
    • 2012-09-23
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多