Reputation: 181
Why in this class move constructor doesn't get fired?
#include <iostream>
#include <algorithm>
using std::cout;
template<class T>
class Movable
{
using value_type = T;
using pointer_type = T*;
const std::size_t init_size_{ 1 };
std::size_t current_size_{ init_size_ };
const value_type init_value_{ 0 };
pointer_type data_ = new value_type[init_size_]{init_value_};
public:
Movable();
Movable(Movable&&);
virtual ~Movable();
};
template<class T>
Movable<T>::Movable(Movable&& dead_meat)
{
data_ = dead_meat.data_;
current_size_ = dead_meat.current_size_;
dead_meat.data_ = nullptr;
dead_meat.current_size_ = 0;
cout << "Move ctor";
}
template<class T>
Movable<T>::Movable()
{
cout << "dflt ctor\n";
}
and in main:
#include "Movable.hpp"
int main()
{
auto m_var{ Movable<int>{} };
return 0;
}
What happens is, only the default constructor is being fired off. Would be really greatful for some explanation on why and how to make it happen.
Upvotes: 1
Views: 55
Reputation: 385108
Completely independent of copies and moves, initialisation like that is permitted to completely elide either. That means the object is constructed in place. It's a sort of standard-permitted "optimisation".
Upvotes: 4