I tested std::forward with CRTP(Curiously Reccursive Template Pattern). However I cannot pass lvalue to it. Please read the following code.
#include <algorithm>
struct DataClassA {};
template <typename Derived>
struct ContainerCRTP {
template <typename Element>
void add(Element&& tag) { }
};
struct Container : public ContainerCRTP<Container> { };
int main()
{
DataClassA data_a;
Container container;
// Case 1: passing lvalue. compile error.
container.add<DataClassA>(data_a);
// Case 2: passing rvalue. It succeeds compiling.
container.add<DataClassA>(std::move(data_a));
}
When I pass lvalue in Case 1, gcc compiler gives me a compile error,
template argument deduction/substitution failed:
cannot convert ‘data_a’ (type ‘DataClassA’) to type ‘DataClassA&&’
On the other hand, when I pass rvalue in Case 2, gcc compiler does not give me compiler error.
How can I give lvalue to container.add<DataClassA>(data_a)
?