2014-02-23 33 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損失。)