久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Qt 中正確使用 C++11 基于范圍的 for 循環

Using C++11 range-based for loop correctly in Qt(在 Qt 中正確使用 C++11 基于范圍的 for 循環)
本文介紹了在 Qt 中正確使用 C++11 基于范圍的 for 循環的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

根據這個演講在 Qt 容器上使用 C++11 范圍基礎 for 時的陷阱.考慮:

According to this talk there is a certain pitfall when using C++11 range base for on Qt containers. Consider:

QList<MyStruct> list;

for(const MyStruct &item : list)
{
    //...
}

根據談話,陷阱來自隱式共享.在引擎蓋下,基于范圍的 for 從容器中獲取迭代器.但是因為容器不是 const,所以 interator 將是非常量的,這顯然足以讓容器分離.

The pitfall, according to the talk, comes from the implicit sharing. Under the hood the ranged-based for gets the iterator from the container. But because the container is not const the interator will be non-const and that is apparently enough for the container to detach.

當您控制容器的生命周期時,這很容易解決,只需將 const 引用傳遞給容器以強制它使用 const_iterator 而不是分離.

When you control the lifetime of a container this is easy to fix, one just passes the const reference to the container to force it to use const_iterator and not to detach.

QList<MyStruct> list;
const Qlist<MyStruct> &constList = list;

for(const MyStruct &item : constList)
{
    //...
}

但是,例如容器作為返回值.

However what about for example containers as return values.

QList<MyStruct> foo() { //... }

void main()
{
    for(const MyStruct &item : foo())
    {
    }
}

這里發生了什么?容器還在復制嗎?直覺上我會說這是為了避免可能需要這樣做?

What does happen here? Is the container still copied? Intuitively I would say it is so to avoid that this might need to be done?

QList<MyStruct> foo() { //... }

main()
{ 
    for(const MyStruct &item : const_cast<const QList<MyStruct>>(foo()))
    {
    }
}

我不確定.我知道它有點冗長,但我需要這個,因為我在大型容器上大量使用基于范圍的 for 循環,所以談話對我來說是正確的.

I am not sure. I know it is a bit more verbose but I need this because I use ranged based for loops heavily on huge containers a lot so the talk kind of struck the right string with me.

到目前為止,我使用輔助函數將容器轉換為常量引用,但如果有更簡單/更短的方法來實現相同的目標,我希望聽到它.

So far I use a helper function to convert the container to the const reference but if there is a easier/shorter way to achieve the same I would like to hear it.

推薦答案

template<class T>
std::remove_reference_t<T> const& as_const(T&&t){return t;}

可能會有所幫助.由于非常量迭代,返回右值的隱式共享對象可以隱式檢測寫入分片(和分離).

might help. An implicitly shared object returned an rvalue can implicitly detect write-shraring (and detatch) due to non-const iteration.

這給你:

for(auto&&item : as_const(foo()))
{
}

它允許您以 const 方式(而且非常清楚)進行迭代.

which lets you iterate in a const way (and pretty clearly).

如果你需要引用生命周期延長來工作,有 2 個重載:

If you need reference lifetime extension to work, have 2 overloads:

template<class T>
T const as_const(T&&t){return std::forward<T>(t);}
template<class T>
T const& as_const(T&t){return t;}

但是迭代 const 右值并關心它通常是一個設計錯誤:它們是丟棄的副本,為什么編輯它們很重要?如果你基于 const 限定的行為非常不同,那會在其他地方咬你.

But iterating over const rvalues and caring about it is often a design error: they are throw away copies, why does it matter if you edit them? And if you behave very differently based off const qualification, that will bite you elsewhere.

這篇關于在 Qt 中正確使用 C++11 基于范圍的 for 循環的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 久久成人国产精品 | 亚洲成人综合在线 | 欧美一区在线看 | 国产精品美女久久久久久久网站 | 成人高清在线 | 国产精品久久久久久久久图文区 | 五月婷亚洲 | 久久久毛片 | 欧美福利三区 | 免费一级黄色电影 | 亚洲一区二区三区在线视频 | 亚洲成在线观看 | 国产精品99久久久久久人 | 久久久久久91 | 国产精品视频久久 | 秋霞在线一区二区 | 在线免费观看一区二区 | 亚洲黄色视屏 | 一二三区av| 亚洲午夜av久久乱码 | 精品真实国产乱文在线 | 久久黄色网 | 精品亚洲一区二区三区 | 欧美精选一区二区 | 色婷婷国产精品 | 国产99久久久国产精品下药 | 黄色在线免费观看 | 亚洲欧洲日韩 | 美女在线观看国产 | 欧美成人h版在线观看 | 国产一区久久 | 精品日韩一区二区三区 | 国产欧美一区二区三区在线看 | 一区二区三区四区av | 黄色免费三级 | 伊人久久在线 | 国产精品视频在线观看 | 成人精品在线观看 | 国产一区二区三区在线视频 | 56pao在线 | 国产亚洲久 |