Why couldn’t push_back be overloaded to do the job of emplace_back?

If T has an explicit conversion constructor, there is different behavior between emplace_back and push_back.

struct X
{
    int val;
    X() :val() {}
    explicit X(int v) :val(v) {}
};

int main()
{
    std::vector<X> v;
    v.push_back(123);    // this fails
    v.emplace_back(123); // this is okay
}

Making the change you suggest would mean that push_back would be legal in that instance, and I suppose that was not desired behavior. I don’t know if this is the reason, but it’s the only thing I can come up with.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)