C++ Armadillo:GCC与VC++ 2013:运算符()和重载

abo*_*man 6 c++ operator-overloading visual-c++ armadillo

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

#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;
}
Run Code Online (Sandbox Code Playgroud)

错误发生在功能上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)'
Run Code Online (Sandbox Code Playgroud)

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

LTh*_*ode 0

根据Ryan 在其评论中链接的SO 帖子,这与MSVC 连接错误 #811334相关,并且应该在 MSVC 2015 中修复。

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