2015-06-15 147 views
-1

我們正在將一些C++代碼從windows移植到mac,並且在使用C++ 11編譯LLVM 6.1時遇到問題。我們在「調用隱式刪除的副本構造器」的地方遇到錯誤。這些錯誤中的一部分出現在我們的代碼中。在LLVM中調用隱式刪除的拷貝構造函數

for (auto it : _unhandledFiles)//ERROR HERE 
{ 
    if (it.first == file) 
    { 
     return true; 
    } 
} 
return false; 

但是,它們也出現在LLVM編譯器的存儲器文件以及矢量文件中。

template <class _Up, class... _Args> 
    _LIBCPP_INLINE_VISIBILITY 
    void 
    construct(_Up* __p, _Args&&... __args) 
    { 
     ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);//ERROR HERE 
    } 


vector<_Tp, _Allocator>::operator=(const vector& __x) 
{ 
if (this != &__x) 
{ 
    __base::__copy_assign_alloc(__x); 
    assign(__x.__begin_, __x.__end_);//ERROR HERE 
} 
return *this; 
} 

在將C++代碼從Windows移植到Mac時,有沒有人遇到過這個錯誤?我覺得好像它是編譯器相關的,必須有一些簡單的修復,我只是不知道,因爲我得到的地方,我實際上不能編輯(內存,矢量等....)的錯誤

+0

'_unhandledFiles'的類型是什麼?請[編輯](http://stackoverflow.com/posts/30850780/edit)您的問題與[SSCCE](http://sscce.org)。 – NathanOliver

+0

'_unhandledFiles'中的東西是可複製的嗎? – Barry

+0

std :: vector _unhandledFiles; –

回答

0

This的代碼線很含糊:

for (auto it : _unhandledFiles)//ERROR HERE 

auto使用模板參數推導,所以

std::string s; 
std::string& sr = sr; 
auto x = sr; 
在上面的代碼 x

被推斷爲是類型std::string,不std::string&的。所以,你的循環相當於:

for (_unhandledFiles::value_type it : _unhandledFiles) 
// aka 
for (auto uhfIt = _unhandledFiles.cbegin(); 
     uhfIt != _unhandledFiles.cend(); 
     ++uhfIt) { 
    _unhandledFiles::value_type it = *uhfIt; // COPY 
    // ... your code here ... 
    it.dtor(); // obviously not, I'm just emphasizing. 
} 

for (_unhandledFiles::value_type& it : _unhandledFiles) 

所以每次循環是複製從_unhandledFiles值。

的解決將是要麼使用迭代或:

for (auto& it: _unhandledFiles) 
---------^ 

---- ----編輯

因爲這會導致混亂的,C++ 14引入decltype(auto)但使用如果rhs不是參考文獻,會引入一個副本。

std::string s; 
std::string& sr = s; 

auto xr1 = sr; // std::string xr1 -> copy 
auto& xr2 = sr; // std::string& xr2 -> reference 
decltype(auto) xr3 = sr; // std::string& xr3 -> reference 

auto xv1 = s; // std::string xv1 -> copy 
auto& xv2 = s; // std::string& xv2 -> reference 
decltype(auto) xv3 = s; // std::string xv3 -> copy 
+1

你是不是指'std :: string&sr = sr'? – 0x499602D2

+0

用以下代替for循環解決了問題。 for(std :: vector :: iterator it = _unhandledFiles.begin(); it!= _unhandledFiles.end(); ++ it) if(it-> first == file) { 返回true; } } return false; –

+0

@ 0x499602D2良好的捕獲,固定。 – kfsone

相關問題