Do not call QString :: operator [] () in temporary

I run clazy in my code and get a warning about this code:

QChar value() const
{
    if (hide_content_)
        return '\0';
    else
        return text()[0];
}

where text()has such a signatureQString text() const;

a warning:

warning: Don't call QString::operator[]() on temporary
      [-Wclazy-detaching-temporary]
            return text()[0];
                   ^

But what does this mean? Is it possible that a temporary object QStringwill be destroyed before the call operator[]?

+6
source share
1 answer

Is it possible that a temporary QString will be destroyed before calling the [] operator?

No, the warning is not about unsafe operation. The behavior here is completely defined as temporary ones are not destroyed until the end of the full expression in which they are created (i.e. until the end of the instruction returnin your case).

: , . , .at(0) .operator[](0). .


, Qt ; , , (, copy-on-write).

, ( ) , , .

, ( operator[]), , ( ) .

, , QString str = "abc";, QChar ch = str[0];, QString operator[], , , , , str[0] = 'x';. , , . const, / .

Clazy , ( , ).


:

+5

All Articles