Consider the following code:
class Test
{
public:
Test()
{
}
Test(const Test &other) noexcept
{
*this = other;
}
Test(Test &&other) noexcept
{
*this = std::move(other);
}
auto operator = (const Test &other) noexcept -> Test&
{
// Make a copy of "other"
// ...
return *this;
}
auto operator = (Test &&other) noexcept -> Test&
{
// Move "other"
// ...
return *this;
}
};
I would like group together move and copy constructors. From what I understood It can be achieved using std::forward like this:
template <class T>
Test(T &&other) noexcept
{
*this = std::forward<T>(other);
}
It looks to be working well if used carefully as follows:
Test test;
Test test1 { test };
Test test2 { std::move(test) };
On the other hand it creates an infinite recursion if I try to instantiate a Test object with a value of type different than Test:
Test test { 1 }; // Creates an infinite recursion
Is there a way to restrict the instantiation of the Test object with the (r||l)values of type Test only?