mardi 28 juillet 2015

Value categories outside of expressions

Outside of the result of an expression, does it make sense to refer to things by value category? For example:

const std::string& GetString()
{
    static std::string test;
    return test;
}

int main()
{
    const std::string& str = GetString();
}

Correct me if I'm wrong, but I believe GetString() returns a prvalue. Does it also make sense to refer to str (variable) as a prvalue, in the context of its type (type + reference + const)? It's a bit hard to explain what I mean here. I know the whole line of code itself is an expression, but for the purposes of evaluating the value category returned by the function itself, I'm treating the right side of the assignment as its own expression. That leaves just the variable itself. After the whole assignment is completed, would it make sense to refer to the variable as a prvalue as well (grasping straws here)?

Personally I find value categories extremely confusing as of C++11 and I want to make sure I understand their scope properly. I haven't seen many examples of value categories that use const references.

Aucun commentaire:

Enregistrer un commentaire