-1

This is NOT a duplicate of "Thou shalt not inherit from std::vector" which is asking about adding "algorithms" not methods.

Obviously there'd be a slicing problem if there were extra members, but there aren't.

There could be some other unexpected behavior if any methods were overiden, but they're not.

The spec says that deleting an object via a base-class pointer with a non-virtual destructor is undefined, but assume I have the self-control not to do that and that if I do, my implementation silently succeeds (as I assume all would; any known exceptions?)

There seems to be no problem with ref's or assignments to std vectors as the only things lost would be the additional methods.

Just off the cuff ideas for new methods that I find myself repeating constantly:

T& allow( size_t x ) { // grows the vector when necessary.
    if ( size() <= x )
        resize( x+1 );
}

T& ok( size_t x ) { // get lvalue, growing the vector when necessary.
    allow( x );
    return operator[]( x );
}

int size() { // return type is int, which is much more convenient; limits you to
             // MAXINT elements but enough for all code I've written in 26 years.
    return (int) vector<T>::size();
}

T& back( int i = 0 ) { // gives you back element, or indexed, gives Nth back.
    return operator[]( size() - i - 1 );
}
Swiss Frank
  • 1,985
  • 15
  • 33
  • 2
    The "right solution" is to use free functions if you just want to add some additional methods. – Justin Sep 12 '18 at 19:01
  • fair enough but why? Any reason besides being technically undefined behavior? – Swiss Frank Sep 12 '18 at 19:03
  • "The spec seems to say that deriving a class with a non-virtual destructor is undefined: Why so?" - It doesn't say that - it says deleting a derived object through a base pointer is undefined if the base destructor is not virtual. –  Sep 12 '18 at 19:04
  • 2
    @SwissFrank Actually not UB. It's perfectly well defined (excepting weird implementations; I'm unsure if `std::vector` is allowed to be `final` for instance). As for why, [this CppCon 2017 talk](https://youtu.be/WLDT1lDOsb4) explains it – Justin Sep 12 '18 at 19:07
  • Personally, I prefer the postfix function call syntax (the member function call syntax), and I am waiting for the day that the proposals which would allow for this get through. – Justin Sep 12 '18 at 19:08
  • the general rule of thumb is to prefer [composition over inheritance](https://en.wikipedia.org/wiki/Composition_over_inheritance). The link provides several reasons why. – user4581301 Sep 12 '18 at 22:22

1 Answers1

1

expr.delete/3:

In a single-object delete expression, if the static type of the object to be deleted is different from its dynamic type and the selected deallocation function (see below) is not a destroying operator delete, the static type shall be a base class of the dynamic type of the object to be deleted and the static type shall have a virtual destructor or the behavior is undefined. In an array delete expression, if the dynamic type of the object to be deleted differs from its static type, the behavior is undefined.

Swordfish
  • 12,971
  • 3
  • 21
  • 43