I want to make a wrapper for a constructor of a class B.
Since some part of the class B is replaceable, I group each implementation of those parts into several class A.
I use perfect forwarding to provide a default implementation of A for constructing a B.
The problem is the wrapper doesn't work. How to fix this?
Thanks.
The following is also at https://godbolt.org/g/AWwtbf
template<typename T>
class A {
public:
A() { _x = 2; }
int _x;
};
template<typename T, typename C=A<T> >
class B {
public:
explicit B(T x, C&& a = A<T>())
: _x(x), _a(a) {
}
T _x;
A<T>& _a;
};
template<typename T, typename C=A<T> >
B<T, A<T>> make_b(T x, C&& c = A<T>()) {
return B<int, A<int>>(x, c);
};
int main() {
B<int, A<int>> b1(1); // this works.
auto b2 = make_b(1); // this doesn't
}
Error:
error: cannot bind rvalue reference of type 'A<int>&&' to lvalue of type 'A<int>'
return B<int, A<int>>(x, c);