[英]Interface designs of std::basic_fstream and std::unique_lock
I'm comparing the two classes, because they both associate with something else. 我正在比较这两个类,因为它们都与其他东西相关联。 std::basic_fstream
has to associate with a file, while std::unique_lock
has to associate with a mutex. std::basic_fstream
必须与文件关联,而std::unique_lock
必须与互斥体关联。 As such, providing an open()
method seems reasonable. 因此,提供open()
方法似乎是合理的。 Still, std::unique_lock
provides no such methods. 尽管如此, std::unique_lock
提供此类方法。 Lazy initialization can be performed anyway via move assignment. 可以通过移动分配执行延迟初始化。 It may thus seem redundant to provide an open()
method. 因此,提供open()
方法似乎是多余的。 std::basic_fstream
, on the other hand, does provide an open()
method. 另一方面, std::basic_fstream
确实提供了open()
方法。 std::basic_fstream
exists before C++11, and it is the only way to perform lazy initialization then. std::basic_fstream
在C ++ 11之前存在,并且这是执行延迟初始化的唯一方法。 Putting aside backward compatibility considerations, would std::basic_fstream::open()
just be removed? 抛开向后兼容性考虑,是否只会删除std::basic_fstream::open()
? Or it still needs to be there for the operation may fail actually? 还是仍然需要在那里进行操作可能实际上会失败? Note that, the open (associate) operation always succeeds with std::unique_lock
(not to be confused with the lock operation). 请注意,打开(关联)操作始终以std::unique_lock
成功(不要与lock操作混淆)。
Your last sentence actually contains a hint: " open
(associate) operation always succeeds with std::unique_lock
". 您的最后一句话实际上包含一个提示:“ open
(关联)操作始终以std::unique_lock
成功”。 This makes it convenient to declare the open-ness as class invariant, establish it in the constructor and throw exceptions on rare failures. 这样可以方便地将开放性声明为类不变,在构造函数中建立开放性,并在极少数失败时引发异常。 Compared that to files: Opening them can and does fail, so declaring open-ness as class invariant doesn't work as well there. 与文件相比:打开文件可能并且确实会失败,因此将开放性声明为类不变性在那儿行不通。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.