2

I tried to use push_back and emplace_back, but all failed.

class Base30 {
public:
    Base30() = delete;
    Base30(int a, int b) : a(a), b(b) {}
    Base30(const Base30& other) = delete;

    int a;
    int b;
};

int main()
{
    vector<Base30> vec;

    Base30 b1 = { 1, 2 };

    //vec.push_back(b1);  // fail

    //vec.emplace_back(1, 2);  // fail

    return 0;
}

How to push_back Base30 into this vector?

rashmatash
  • 1,699
  • 13
  • 23
L.SG
  • 121
  • 1
  • 5
  • 8
    You need to make `Base30` *moveable* instead. – Yksisarvinen Dec 19 '19 at 12:22
  • 1
    when you delete auto-generated functions, or define a custom destructor you'll need to define/delete all other auto generated functions yourself. So, define any constructors, delete copy-constructor and copy-assignment operator (if your class is non-copyable), define move-constructor and move-assignment operator (or delete them if the class is non-movable). Lastly define a destructor. If you've done all this courtesy of wonderfull C++ language, you can finally push stuff in a vector (or `emplace_back`) them. – rashmatash Dec 19 '19 at 12:25
  • 2
    The `emplace_back` line works just fine for me: https://godbolt.org/z/oHu7vF . (however, @Yksisarvinen 's suggestion is a good one) – parktomatomi Dec 19 '19 at 12:40

0 Answers0