Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are std::future and std::promise not final?

I wounder why the classes std::future and std::promise are not marked with the final specifier. The destructor is not virtual so why was final not added? What was (is) the rationale?

like image 984
Sonic78 Avatar asked Apr 17 '19 10:04

Sonic78


1 Answers

Have a look at this contrived (admittedly nonsensical) example with std::vector:

template <class T>
struct Example : private std::vector<T> {
   void doStuff(const T& t) { this->push_back(t); }
   T retrieveStuff() { return this->operator[](0); }
};

Example<int> e;

e.doStuff(42);
std::cout << e.retrieveStuff() << "\n";

This works, you can't get into UB due to std::vector::~vector not being virtual because you can't delete an object through a base class pointer (public inheritance is needed there).

The inheritance here is just an implementation detail. Not recommended practice, but people probably did and do this. Once the decision is made to not break existing code by making std::vector or other container types final, it makes sense to stick to that with different vocabulary types like std::promise or std::future.

like image 135
lubgr Avatar answered Oct 07 '22 18:10

lubgr