Here is the code snippet:
#include <iostream>
#include <vector>
template <typename T>
class Point
{
public:
Point() = default;
Point(T x, T y):x_(x),y_(y){}
//Point(const Point&) = delete;
Point(Point&&) = default;
T x_;
T y_;
};
#define INIT_VECTOR
int main ()
{
#ifdef INIT_VECTOR
std::vector<Point<int>> myvector={{1,1},{2,2}}; //why it does not compile?
#else
std::vector<Point<int>> myvector; //whereas, this works well.
myvector.reserve(5);
#endif
myvector.emplace_back();
auto& point = myvector.back();
point.x_ = 6;
point.y_ = 9;
std::cout << "myvector contains:" << std::endl;
for (auto& pt: myvector)
{
std::cout << ' ' << pt.x_ << "," << pt.y_ << std::endl;
}
return 0;
}
It's clear that Point<T>
has no copy constructor, whereas it has move constructor.
My question is why std::vector<Point<int>> myvector={{1,1},{2,2}};
does not compile?
Could somebody shed some light on this matter? I thought and thought, but I still can't figure out why.