vector and const

后端 未结 10 1657
無奈伤痛
無奈伤痛 2020-12-03 07:13

Consider this

 void f(vector& p)
 {
 }
 int main()
 { 
  vector nonConstVec;
  f(nonConstVec);
 }

The followi

10条回答
  •  失恋的感觉
    2020-12-03 07:58

    As others have said, conversions aren't applied to the template parameters. Put another way,

    vector
    

    ...and:

    vector
    

    ... are completely different types.

    If you are trying to implement const-correctness in regard to f() not modifying the contents of the vector, this might be more along the lines of what you're looking for:

    void f(vector::const_iterator begin, vector::const_iterator end)
    {
      for( ; begin != end; ++begin )
      {
        // do something with *begin
      }
    }
    
    int main()
    {
      vector nonConstVec;
      f(nonConstVec.begin(), nonConstVec.end());
    }
    

提交回复
热议问题