2021-01-27 13:42:22 +01:00
``++std::forward++`` forwards lvalues either as lvalues or as rvalues based on its template argument.
2021-02-02 15:02:10 +01:00
2021-02-02 16:54:43 +01:00
``++std::forward++`` should always take as a non-template argument a forwarding reference which is defined by the standard as:
2021-02-02 15:02:10 +01:00
2021-02-02 16:54:43 +01:00
_rvalue reference to a cv-unqualified template parameter that does not represent a template parameter of a class template._
2020-12-21 15:38:52 +01:00
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
If you don’ t pass forwarding reference as an argument to ``++std::forward++`` S5417 will be triggered.
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
If you don’ t pass the template parameter referred to by the forwarded reference or the ``++decltype++`` of the forwarded expression this rule will be triggered.
2020-12-21 15:38:52 +01:00
== Noncompliant Code Example
----
template <class T>
void g(T&& t);
template <class T>
void f(T&& t) {
g(std::forward<T&&>(t)); // Noncompliant
g(std::forward<T&>(t)); // Noncompliant
}
----
== Compliant Solution
----
template <class T>
void g(T&& t);
template <class T>
void f(T&& t) {
g(std::forward<T>(t)); // Compliant
}
struct StrWrapper {
std::string s = "rand";
std::string getStr() && {
return s;
}
std::string& getStr() & {
return s;
}
};
template <class T>
void fstr(T&& str);
template <class T>
void wrapper(T&& strWrapper ) {
fstr(forward<decltype(forward<T>(strWrapper).getStr())>(forward<T>(strWrapper).getStr())); // Compliant
}
----