I am trying to use a member function of a template to set a value inside my class. I wanted to use a universal reference so that I could accept any variant of the correct type (e.g. T , T& , T&& , const T , const T& , const T&& )
However, it looks like my member function will only accept rvalues, unlike a free function that accepts a universal reference.
template <typename T> class Foo{ public: void memberURef(T&& t){ val = std::forward<T>(t); } private: T val; }; template <typename T> void freeURef(T&& t){ } int main() { int lval = 1; const int clval = 1; freeURef(lval); // fine freeURef(clval); // fine Foo<int> foo; foo.memberURef(2); foo.memberURef(lval); //error: cannot bind 'int' lvalue to 'int&&' foo.memberURef(clval); //error: no matching function for call to 'Foo<int>::memberURef(const int&)' return 0; }
c ++ c ++ 11 templates universal-reference
Jason powers murray
source share