I have some class and wrapper around it. For example:
#include <iostream>
#include <string>
template<typename T>
class inner
{
public:
void f(T& v) { std::cout<<"lvalue: "<<v<<std::endl;}
void f(T&& v) { std::cout<<"rvalue: "<<v<<std::endl;}
};
template<typename T>
class wrapper
{
public:
template<typename _T>
void f(_T&& v) { i.f(std::forward<T>(v)); } //(1)
private:
inner<T> i;
};
int main()
{
wrapper<std::string> c;
//inner<std::string> c;
c.f("r");
std::string s = "l";
c.f(s);
}
In the case when c is inner output is correct:
rvalue: r
lvalue: l
But when c is wrapper output is not correct:
rvalue: r
rvalue: l
Why l-value became r-value?
And what the difference if wrapper's f definition on line (1) would be:
template<typename _T>
void f(_T v) { i.f(std::forward<T>(v)); } //without &&
Aucun commentaire:
Enregistrer un commentaire