2014-02-23 38 views
6

我想开发一个使用Armadillo C++库的Linux/Win64应用程序。以下代码在GCC-4.7中编译,但无法使用Armadillo提供的VS工程文件在Visual Studio 2013中编译。C++犰狳:GCC与VC++ 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; 
} 

错误ocurs与功能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()

+0

正如一个额外的数据点,Clang说的是什么? – rubenvb

+0

它在Clang 3.4下编译得很好。这看起来像MS VC++中的错误,因为第一个建议显然是假的(“_...可以是const arma :: subview_col arma :: Col :: operator()(const arma :: span&)const_” )。看看Armadillo源代码,整数参数1和'arma :: span'之间没有隐式转换。请参阅include/armadillo_bits/span.hpp中的第58行,其中'span'类的相关构造函数标记为“explicit”。 MS VC++的其他建议也是假的,因为Col :: fixed类继承自Col类并重新定义了operator()。 – mtall

+0

这实际上是一个MSVC错误,其中'explicit'被忽略;有关更多信息,请参阅http://stackoverflow.com/questions/20498142/visual-studio-2013-explicit-keyword-bug。 – ryan

回答

0

这是关系到MSVC connect bug #811334为每SO post瑞恩在这里他的评论链接,并将其固定在MSVC 2015年

(该缺陷是MSVC无视构造器的explicit关键字 - 这是有关到链接的SO帖子的代码,但不完全相同,因为链接的SO帖子和报告处理转换运营商的explicit损失。)