How to make argument an rvalue and an lvalue?

In the book of Scott Meyers ' Effective C++ here is an example of such a function using a universal reference:
template<typename Container, typename Index>
decltype(auto) authAndAccess(Container&& c, Index i) {
 return std::forward<Container>(c)[i];
}

Universal reference as I understand it is used for if you pass an lvalue we have obtained a reference to the container, and in the case of an rvalue, we received a copy of the container. The problem is that std::forward in any case returns a reference to the container element. That is:
decltype(auto) variable = fnt(vector<string>{"one", "two"}, 0);

A type variable is string& what is essentially a reference to a local function object, which leads to a SIGSEGV. How to make a function to return a copy of the element in the case and an rvalue reference to the element if passed an lvalue.
March 19th 20 at 08:36
1 answer
March 19th 20 at 08:38
Universal reference allows you to refer to both types, using the rules of inference of the reference type for the template parameter. About forward I already wrote in a comment. There is some difficulty to implement your wishlist in one function. Because the container type should influence the type of the return value of this container. The easiest option is to write 2 overloads:
template<typename Container, typename Index>
auto authAndAccess(Container&& c, Index i) {
 return std::forward<Container>(c)[i];
}

template<typename Container, typename Index>
decltype(auto) authAndAccess(Container& c, Index i) {
 return std::forward<Container>(c)[i];
}

In the first case, we return by value, the second the link.

Find more questions by tags C++