Handling lvalue and rvalue values ​​evenly

I have two related questions, both related to how to handle lvalue and rvalue links evenly.

Virtual function case:

struct Foo {
    Object m_object;

    virtual void bar(SomeRefTypeOfObject object);
};

Basically, I want to receive SomeRefTypeOfObjectwhich can store both lvalue, and rvalue the link to Object. baris a big function, and it will use one operator m_object = object;to store the value Object(copy or move operation depending on the type of stored link). The reason is because I want to avoid two functions bar(for each reference type). Is there anything in the standard library that can do this conveniently and efficiently, or do I need to roll my own solution? If I need to roll my decision, what would a good implementation look like?

Template function case:

struct Foo {
    Object m_object;

    template <...>
    void bar(... object);
};

I would like to have a template barthat can be called with any type Objector its derived classes / other objects that can be converted to Object(for example, if there barwere two overloaded functions with parameters const Object &and Object &&), but are created using only const Object & and Object &&. Thus, I do not want to be barcreated for every derived type. What is the clearest way to do this? I suppose I will need some form of SFINAE.

: m_object = object; , bar. , , , , / ( , , ). , , ( , ).

+4
3

(, , ), :

#include <iostream>
#include <type_traits>
#include <utility>

struct Object { 
    Object() {
        std::cout << "Object()" << std::endl;
    }
    Object(const Object &) {
        std::cout << "Object(const Object &)" << std::endl;
    }
    Object(Object &&) {
        std::cout << "Object(Object &&)" << std::endl;
    }
    Object &operator =(const Object &) {
        std::cout << "Object &operator =(const Object &)" << std::endl;
        return *this;
    }
    Object &operator =(Object &&) {
        std::cout << "Object &operator =(Object &&)" << std::endl;
        return *this;
    }
};

template <class T>
struct RLReferenceWrapper {
    bool is_rvalue;
    T *pnull;
    const T& cref;
    T&& rref;
    RLReferenceWrapper(T&& rref): is_rvalue(true), pnull(nullptr), cref(*pnull), rref(std::move(rref)) { }
    RLReferenceWrapper(const T& cref): is_rvalue(false), pnull(nullptr), cref(cref), rref(std::move(*pnull)) { }

    template <class L>
    void Visit(L l) {
        if (is_rvalue) {
            l(std::move(rref));
        } else {
            l(cref);
        }
    }
};

struct Foo {
    Object m_object;

    virtual void bar(RLReferenceWrapper<Object> wrapper) {
        wrapper.Visit([this](auto&& ref){ 
            m_object = std::forward<std::remove_reference_t<decltype(ref)>>(ref);
        });
    }
};

int main() {
    Foo f;
    f.bar(Object{});
    Object o2;
    f.bar(o2);
}

:

Object()
Object()
Object &operator =(Object &&)
Object()
Object &operator =(const Object &)

[live demo]

+1

( ):

template <class T> struct movable{};
template <class T> struct movable<T const&>
{
  movable( movable const& ) = delete;
  movable( T const& r ): ref_{ const_cast<T&>(r) }, movable_{false} {}
  movable( T&& r ): ref_{r}, movable_{true} {}

  auto get() const -> T const& { return ref_; }
  auto move() -> T&& { if( !movable_ ) throw std::runtime_error{"bad move!"}; movable_ = false; return std::move(ref_); }

  template <class V>
  void assign_or_move_to( V& v ) { if( movable_ ) v = move(); else v = get(); }

private:
  T&   ref_;
  bool movable_;
};

struct Foo {
  Object m_object;

  virtual void bar( movable<Object const&> object_ref )
  {
    auto& object = object_ref.get();

    // ...

    object_ref.assign_or_move_to( m_object );

    // or

    if( something ) m_object = object_ref.move();
  }
};

, , , , , ...

0

What about

struct Foo {
    Object m_object;

    void bar(Object& o) { bar_impl(o); }
    void bar(Object&& o) { bar_impl(std::move(o)); }

    template <typename T>
    void bar_impl(T&& object) { 
        // snip ...
        m_object = std::forward<T>(object);
    }
};

I think:

  • Creates only an instance for Object & & Object &&
  • Only one instance of a long function
  • Allows the template to choose copy or move depending on what has passed.

It does not have a wrapper object or anything else, but I think that OK b / c is T&&not rvalue as such, it is a universal link, as described here: https://isocpp.org/blog/2012/11/universal -references-in-c11-scott-meyers (thanks to Scott!)

0
source

Source: https://habr.com/ru/post/1686626/


All Articles