std::error_code 和 std::error_condition 是否由相同的值和相同的类别构造,始终是等价
Are std::error_code and std::error_condition constructed from same value and same category always equivalent?
我知道error_code是系统依赖的,error_condition是系统无关的,但这是否意味着如果我们在构造它们时指定值和类别,它们会有所不同。例如:
std::error_code ecode(1, std::system_category());
std::error_condition econd(1, std::system_category());
if (ecode == econd) // is this condition always true no matter what platform we are in??
以上情况在macOS的XCode中是正确的,所以我想知道如果我们在其他平台(例如Windows(中是否总是如此。
如果是这样,为什么会这样ecode
是系统依赖的,econd
是系统独立的?
他们不是。 错误代码和条件的相等性由类别成员函数"等效"确定,您可以编写一个永远不会使任何代码和条件相等的类别。 例如:
#include <system_error>
#include <iostream>
struct cat_type : std::error_category
{
const char *name() const noexcept { return "name"; }
std::string message(int) const { return "message"; }
bool equivalent(int, const std::error_condition &) const noexcept { return false; }
bool equivalent(const std::error_code &, int) const noexcept { return false; }
} cat;
int main() {
std::error_code ecode(1, cat);
std::error_condition econd(1, cat);
std::cout << (ecode == econd) << 'n';
}
此程序打印 0,因为调用了每个等效重载,并且它们都返回 false,因此它们不相等。
但是,对于std::system_category
具体而言,标准要求equivalent
函数具有默认行为(请参阅 N4800 第 18.5.2.5 节 syserr.errcat.objects 第 4 段(,并且由于默认行为是将具有相同类别和值的代码和条件视为相等,因此它们将进行比较。
相关文章:
- 修复第三方代码:"error: ‘enable_if’ in namespace ‘std’ does not name a template type"
- 为什么GCC可以编译std::exception("some error msg")而没有错误?
- \main.cpp|103|error: 与"operator<<"不匹配(操作数类型为"std::ostream {aka std::basic_ostream<char&g
- 将static_pointer_cast添加到<Derived> std::list<shared_ptr<Base>> 会导致称为 error 的纯虚拟方法
- 返回的向量定义为 "std::vector<<error-type>, std::allocator<<error-type>>>
- boost::spirit::qi::phrase_parser() into std::map error
- std::map linker error ndk r8c with APP_STL := gnustl_static
- std::vector for_each error C3867函数调用缺少参数列表
- 我在检查字符串时得到"Invalid utf 8 error",但当我使用 std::cout 时似乎是正确的
- 使用 std::string 时"error: no match for ‘operator<<"
- std::stoi, std::setbase, and an std::out_of_range error
- C++ std::bad_alloc error
- push_back std::pair into std::vector error
- 我不断收到来自Xcode的错误,说" Apple Mach-O Linker (Id) Error "readIt(std::_1::basic_ifstream<char,std::_1::c
- std::async([](){ std::cout<< "Hello " ; }) build error
- C++ std::bad_alloc error with std::vector
- Linker error for functions expecting const std::string &
- 初始化标准::auto_ptr:"error: no match for call to ‘(std::auto_ptr<int>) (int*)’"
- 获得"error C2039: 'value_type' : is not a member of 'std::greater<_Ty>'"
- openfoam: flex yyin.rdbuf(std::cin.rdbuf() error