簡體   English   中英

為什么const范圍不是基於使用const_iterator?

[英]Why doesn't const range based for use const_iterator?

如果我只想通過對象公開一個const迭代器:

class MyList
{
  public:
    const_iterator begin() const;
    const_iterator end() const;
  private:
    iterator begin();
    iterator end();
};

看來我應該能夠使用基於以下范圍的const版本:

MyList list;
...
for(const auto & value : list)
{
}

編譯器抱怨beginend都是私有的。 為什么不使用const_iterator版本?

在訪問檢查之前完成重載解析,以避免僅通過更改訪問指定符來神奇地破壞代碼。

之后的表達式(其類型)會因此而被忽略。 如果需要,編譯器將嘗試在之后找到有效且明確的轉換序列。

因此,選擇非const -object的beginend ,然后編譯器偶然發現那個大的private -sign。

暫無
暫無

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

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