lundi 26 avril 2021

A use-case for r-value members

I've discovered an interesting piece of code and I wonder if it is UB or not? At least, according to cppreference it should not be. Is it a valid case for using an r-value reference?

The lifetime of a temporary object may be extended by binding to a const lvalue reference or to an rvalue reference...

Something similar was discussed in this post, but it does not fully address my case.

template <typename T>
class ResourceLocator {
public:
    static void load(ResourceLocator<T>&& instance) {
        instance_ = std::move(instance);
    }

protected:
    static ResourceLocator<T>&& instance_;
    // static ResourceLocator<T>& instance_; // does not extend lifetime
    // static const ResourceLocator<T>&& instance_; // const is too limiting

Aucun commentaire:

Enregistrer un commentaire