簡體   English   中英

find_if()在const函數中返回意外的迭代器類型

[英]find_if() returns unexpected iterator type in const function

以下是一些代碼的簡化版本,這些代碼在g ++ 4.8中產生以下錯誤:

x.cpp: In member function ‘void Container::find() const’:
x.cpp:11:71: error: 
conversion from ‘__gnu_cxx::__normal_iterator<const Element*, std::vector<Element> >’ to non-scalar type ‘std::vector<Element>::iterator {aka __gnu_cxx::__normal_iterator<Element*, std::vector<Element> >}’ requested vector<Element>::iterator it = find_if(v.begin(), v.end(), f);

這是代碼:

#include <algorithm>
#include <vector>
using namespace std;

struct Element{};
struct Functor{ bool operator()(const Element & ){return false;}; };
struct Container{
   vector<Element> v;
   void find() const {
      Functor f;
      vector<Element>::iterator it = find_if(v.begin(), v.end(), f);
   }
};
int main() { return 0; }

刪除find()函數上的const限定符后,錯誤消失。 錯誤的原因是什么? 鼓勵引用標准。 謂詞等的邏輯已刪除,以關注關注的問題。 clang 3.4產生類似的結果。

由於vconstContainer::find (其本身是const ),類型v.begin()v.end()並因此返回類型find_if ,是vector<Element>::const_iterator

暫無
暫無

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

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