Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/error/error condition"

From cppreference.com
< cpp‎ | error
m (for now, no example (although both Visual Studio and Clang++ have these), so there will be examples.)
m (Notes: in-house fmt.)
 
(19 intermediate revisions by 9 users not shown)
Line 1: Line 1:
 
{{cpp/title|error_condition}}
 
{{cpp/title|error_condition}}
{{cpp/error/error_condition/sidebar}}
+
{{cpp/error/error_condition/navbar}}
{{ddcl | header=system_error | notes={{mark since c++11}} | 1=
+
{{ddcl|header=system_error|since=c++11|1=
 
class error_condition;
 
class error_condition;
 
}}
 
}}
  
{{tt|std::error_condition}} is a platform-independent error code. Like {{cpp|std::error_code}}, it is uniquely identified by an integer value and a {{cpp|std::error_category}}, but unlike {{cpp|std::error_code}}, the value is not platform-dependent.
+
{{tt|std::error_condition}} holds a platform-independent value identifying an error condition. Like {{lc|std::error_code}}, it is uniquely identified by an integer value and a {{lc|std::error_category}}, but unlike {{lc|std::error_code}}, the value is not platform-dependent.
  
A typical implementation holds one integer data member (the value) and a pointer to an {{cpp|std::error_category}}.
+
A typical implementation holds one integer data member (the value) and a pointer to an {{lc|std::error_category}}.
 
   
 
   
 
===Member functions===
 
===Member functions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list mem ctor | cpp/error/system_error/error_condition/error_condition | constructs an error_condition}}
+
{{dsc inc|cpp/error/error_condition/dsc constructor}}
{{dcl list mem fun | cpp/error/system_error/error_condition/operator{{=}} | replaces the contents of an error_condition}}
+
{{dsc inc|cpp/error/error_condition/dsc operator{{=}}}}
{{dcl list mem fun | cpp/error/system_error/error_condition/assign | replaces the contents of an error_condition}}
+
{{dsc inc|cpp/error/error_condition/dsc assign}}
{{dcl list mem fun | cpp/error/system_error/error_condition/clear | sets the error_condition to value 0 in generic_category }}
+
{{dsc inc|cpp/error/error_condition/dsc clear}}
{{dcl list mem fun | cpp/error/system_error/error_condition/value | obtains the value of the error_condition}}
+
{{dsc inc|cpp/error/error_condition/dsc value}}
{{dcl list mem fun | cpp/error/system_error/error_condition/category | obtains the error_category for this error_condition}}
+
{{dsc inc|cpp/error/error_condition/dsc category}}
{{dcl list mem fun | cpp/error/system_error/error_condition/message | obtains the explanatory string for this error_condition}}
+
{{dsc inc|cpp/error/error_condition/dsc message}}
{{dcl list mem fun | cpp/error/system_error/error_condition/operator bool | checks if the value is non-zero }}
+
{{dsc inc|cpp/error/error_condition/dsc operator bool}}
{{dcl list end}}
+
{{dsc end}}
  
 
===Non-member functions===
 
===Non-member functions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list fun | cpp/error/system_error/error_condition/operator_lt | title=operator< | provides ordering for error_conditions}}
+
{{dsc inc|cpp/error/error_condition/dsc operator_cmp}}
{{dcl list fun | cpp/error/system_error/error_condition/operator_eq | title=operator==<br>operator!= | compares error_conditions and error_codes}}
+
{{dsc end}}
{{dcl list template | cpp/error/system_error/error_condition/dcl list make_error_condition}}
+
{{dcl list end}}
+
  
 
===Helper classes===
 
===Helper classes===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/error/system_error/error_condition/dcl list is_error_condition_enum}}
+
{{dsc inc|cpp/error/error_condition/dsc is_error_condition_enum}}
{{dcl list end}}
+
{{dsc inc|cpp/error/error_condition/dsc hash}}
 +
{{dsc end}}
 +
 
 +
===Notes===
 +
The [[cpp/error/error_condition/operator_cmp|comparison]] between a {{lc|std::error_code}} and a {{tt|std::error_condition}} is defined by their error categories. Notably, an error condition of {{lc|std::generic_category}} may compare equal to an error code of a specific category (e.g. {{lc|std::system_category}}), if they represent the same kind of error.
 +
 
 +
A {{lc|std::errc}} value can be compared to an error code via implicit conversion to {{tt|std::error_condition}}.
 +
 
 +
{{example
 +
|code=
 +
#include <cerrno>
 +
#include <iostream>
 +
#include <system_error>
 +
#include <Windows.h>
 +
 
 +
int main()
 +
{
 +
    std::error_code ec{ERROR_FILE_EXISTS, std::system_category()};
 +
    std::error_condition econd{EEXIST, std::generic_category()};
 +
 
 +
    std::cout.setf(std::ios::boolalpha);
 +
    std::cout << (ec == econd) << '\n'; // typically true<!-- false with libc++, because libc++ treats generic_category() and system_category() as being equivalent, even on windows. See also https://github.com/llvm/llvm-project/pull/93101 -->
 +
    std::cout << (ec == std::errc::file_exists) << '\n'; // ditto
 +
    std::cout << (ec == make_error_code(std::errc::file_exists)) << '\n'; // false:
 +
                                                                    // different category
 +
}
 +
|p=true
 +
|output=
 +
true
 +
true
 +
false
 +
}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/error/system_error/dcl list error_code}}
+
{{dsc inc|cpp/error/dsc error_code}}
{{dcl list template | cpp/error/system_error/dcl list error_category}}
+
{{dsc inc|cpp/error/dsc error_category}}
{{dcl list end}}
+
{{dsc inc|cpp/error/errc/dsc make_error_condition}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 03:42, 9 June 2024

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
 
Defined in header <system_error>
class error_condition;
(since C++11)

std::error_condition holds a platform-independent value identifying an error condition. Like std::error_code, it is uniquely identified by an integer value and a std::error_category, but unlike std::error_code, the value is not platform-dependent.

A typical implementation holds one integer data member (the value) and a pointer to an std::error_category.

Contents

[edit] Member functions

constructs an error_condition
(public member function) [edit]
replaces the contents
(public member function) [edit]
replaces the contents
(public member function) [edit]
sets the error_condition to value 0 in generic_category
(public member function) [edit]
obtains the value of the error_condition
(public member function) [edit]
obtains the error_category for this error_condition
(public member function) [edit]
obtains the explanatory string
(public member function) [edit]
checks if the value is non-zero
(public member function) [edit]

[edit] Non-member functions

(removed in C++20)(removed in C++20)(C++20)
compares error_conditions and error_codes
(function) [edit]

[edit] Helper classes

identifies an enumeration as an std::error_condition
(class template) [edit]
hash support for std::error_condition
(class template specialization) [edit]

[edit] Notes

The comparison between a std::error_code and a std::error_condition is defined by their error categories. Notably, an error condition of std::generic_category may compare equal to an error code of a specific category (e.g. std::system_category), if they represent the same kind of error.

A std::errc value can be compared to an error code via implicit conversion to std::error_condition.

#include <cerrno>
#include <iostream>
#include <system_error>
#include <Windows.h>
 
int main()
{
    std::error_code ec{ERROR_FILE_EXISTS, std::system_category()};
    std::error_condition econd{EEXIST, std::generic_category()};
 
    std::cout.setf(std::ios::boolalpha);
    std::cout << (ec == econd) << '\n'; // typically true
    std::cout << (ec == std::errc::file_exists) << '\n'; // ditto
    std::cout << (ec == make_error_code(std::errc::file_exists)) << '\n'; // false:
                                                                     // different category
}

Possible output:

true
true
false

[edit] See also

holds a platform-dependent error code
(class) [edit]
base class for error categories
(class) [edit]
creates an error condition for an errc value e
(function) [edit]