我正在尝试使用 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;
}
函数 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()
。
最佳答案
这与 MSVC connect bug #811334 有关根据 SO post Ryan 在此处链接了他的评论,应该会在 MSVC 2015 中修复。
(错误在于 MSVC 忽略了构造函数中的 explicit
关键字——它与链接的 SO 帖子的代码相关,但不完全相同,因为链接的 SO 帖子和报告处理explicit
对转换运算符的损失。)
关于C++ Armadillo : GCC vs VC++2013: Operator () and overloading,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21963867/