dimanche 5 mai 2019

Destruction of local function variables vs construction of the return value

There are code examples that rely on the fact that local function variables are destroyed after the return value has been created, for example:

1 https://stackoverflow.com/a/4541470/8414561

std::string demangle(const char* name)
{
    int status = -4;
    std::unique_ptr<char, void(*)(void*)> res {
        abi::__cxa_demangle(name, NULL, NULL, &status),
        std::free
    };
    return (status==0) ? res.get() : name;
}

2 Timing of scope-based lock guards and return values

class C {
    mutable std::mutex _lock;
    map<string,string> deep_member;
public:
    auto get_big_lump()
    {
        std::unique_lock<std::mutex> lock(_lock);
        return deep_member;
    }
};

Where does the standard specify this order?

Aucun commentaire:

Enregistrer un commentaire