Argument forwarding function and just does nothing

For the case of the default template, I need a function that does nothing, but simply redirects everything it receives as an argument. In particular, links, constant, etc. Must be saved. The transparent(/* something */) entry should be completely equivalent to the /* something */ entry.

Is the following definition of function correctly defined to achieve this?

 template <class S> decltype(auto) transparent (S && s) { return std::forward<S> (s); } 
+7
c ++ perfect-forwarding c ++ 14
source share
2 answers

Add constexpr and it is as good as it is. prvalues ​​will give x values; However, this has not improved since prvalues ​​and xvalues ​​cannot be distinguished using overload resolution.

You cannot correctly send 0 as a null pointer constant or string literals as initializers, but the only way to achieve this is with a macro (this is not what you are going to do).

+3
source share

Your implementation is fine, but here are some considerations:

If the transparent () call passes rvalue std :: string, then it is displayed on std :: string, and std :: forward ensures that the value of the rvalue reference is returned.

If the transparent () call passes const lvalue std :: string, then S is displayed on const std :: string &, and std :: forward ensures that a Link const lvalue returns

If the transparent () call does not pass const const lvalue std :: string, then S is displayed on std :: string &, and std :: forward ensures that the non-const lvalue reference returns

But why do you need this? Common usage of std :: forward in templates for such a warpar:

 template<class T> void wrapper(T&& arg) { foo(std::forward<T>(arg)); // Forward a single argument. } 
+2
source share

All Articles