mardi 3 mars 2020

Which variant of operator [] of std::vector gets called when the following line is executed and why?

According to C++ Reference Operator[] of std::vector has 2 variants

  1. reference operator[] (size_type n);
  2. const_reference operator[] (size_type n) const;

When we execute the following line which of the above variant gets called?

std::vector<int> vlist;
vlist[0] = 7;

My thought, the 1st variant should not be called as I am assigning rvalue to an non-const lvalue reference, the way int& x = 7; is illegal.

Aucun commentaire:

Enregistrer un commentaire