簡體   English   中英

調用 LLVM 中隱式刪除的復制構造函數(將代碼從 windows 移植到 mac)

[英]Call to implicitly-deleted copy constructor in LLVM(Porting code from windows to mac)

我們正在將一些 c++ 代碼從 windows 移植到 mac,並且在使用 c++11 用 LLVM 6.1 編譯它時遇到了問題。 我們在“調用隱式刪除的復制構造函數”的所有地方都遇到了錯誤,其中一些錯誤在我們的代碼中彈出。

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 之前,有沒有人遇到過這個錯誤? 我覺得好像它與編譯器有關,並且必須有一些我不知道的簡單修復,因為我在無法實際編輯的地方(內存、向量等...)遇到錯誤

這行代碼非常含糊:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM