c++ - 为什么 std::future 和 std::promise 不是最终的?

标签 c++ c++11 c++14 language-lawyer

我不知道为什么要上课 std::futurestd::promise没有用 final 说明符标记。 destructornot 虚拟的,为什么没有添加 final?理由是什么?

最佳答案

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";

这可行,由于 std::vector::~vector,您无法进入 UB不是 virtual因为您不能通过基类指针删除对象(public 需要继承)。

这里的继承只是一个实现细节。不推荐的做法,但人们可能已经这样做了。一旦做出不破坏现有代码的决定,通过制作 std::vector或其他容器类型 final , 坚持使用不同的词汇类型是有意义的,比如 std::promisestd::future .

关于c++ - 为什么 std::future 和 std::promise 不是最终的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55725646/

相关文章:

c++ - 如果 const char* 从函数返回,则 LPCWSTR 损坏

C++ 正则表达式 : matching a label followed by comma separated values

c++ - 使用可变参数模板的显式模板实例化

c++ - POD成员默认初始化不带大括号

c++ - 宏与模板结合生成Object

c++ - 在 C++ 中将文本文件读入 std::string 的单行代码?

性能的 C++ 模板?

c++ - 安置新的和异常(exception)

c++ - 我可以在 lambda 中使用 constexpr 值而不捕获它吗?

c++ - 可用作数据类型的变量。