0
votes

I'm writing a custom implementations of a container. I'm using a std::vector in which I append elements in the end with an emplace_back functions.

In a push function I'm using custom_vector.emplace_back(std::forward<X>(value));

struct Node {
    T value;
                
    template <typename X>
    Node(X &&x) : value{std::forward<X>(x)} {}
};

I'm not getting what is really doing std::forward. In this way I can avoid to write both the copy and move constructor? Can some one explain the behaviour of std::forward?

In this case is working everything is working but only con emplace back and not con push back. It makes sense?

You have both move-constructor and copy-constructor... they are just implicitly-declared by the compiler for you automatically. (As long as T is copyable and movable).super
Ok that they are implicitly declared. But since I create a new node only with the push function, what forward does in this case?carlo97
Do you know what a forwarding reference is? I think std::forward may not carry much meaning before understanding forwarding references.Drew Dormann
Yes a forward reference can take both lvalue and rvalue references. But what I'm not understanding is when is called one and when the other. And why.carlo97