vendredi 23 décembre 2016

VC++ auto specifier assuming reference qualifier for vector

On using auto specifier to create a variable that is initialized with the return value from vector::back(), the variable is having a reference qualifier.

int main()
{
    bool b = true;
    bool & j = b;
    auto k = j;
    k = false; // k is of int type. So, j and i are unaffected. 

    std::vector< bool > vec = { true };
    auto l = vec.back();
    vec.pop_back();
    l = false;  // I get a debug assertion here.
}

The k variable has bool as it's type, but variable l has the type std::_Vb_reference<std::vector< bool, std::allocator< bool >>::_Alty >

If I use int instead of bool, auto specifier works for vector as well.

Is it a bug in VC++ ? I am using Microsoft Visual C++ 2013.

Aucun commentaire:

Enregistrer un commentaire