OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Why does std::make_error_code(std::errc) exist?

  • Thread starter Thread starter Brad Spencer
  • Start date Start date
B

Brad Spencer

Guest
I understand the difference between std::error_code and std::error_condition, and I understand that std::errc is an "error condition" enum and not an "error code" enum. I understand how to compare resulting std::error_code values against "error conditions" and so on. I have read Chris Kohlhoff's blog post about these types several times over the years. I have used these types and extensions to them in production code.

However, I do not understand why std::make_error_code(std::errc) exists.

It doesn't seem to be necessary for comparing actual std::error_code values against std::error_condition values: there is std::make_error_condition(std::errc) and conversions to std::error_condition and special comparison overloads for all of that.

The function's existence is especially puzzling because std::is_error_code_enum_v<std::err> is false for std::errc. Presumably that is to prevent implicitly converting std::errc to std::error_code via std::make_error_code(std::errc), but it isn't clear to me why preventing that is desirable when std::make_error_code(std::errc) exists. In other words, if you shouldn't make an std::error_code out of std::errc, why is there a function that does just that? And if you should, why is that implicit constructor disabled?

Is it std::errc a special case because code often wants to actually produce a real std::error_code in the std::generic_category() with an std::errc value? In other words, is std::errc in some ways both an "error code" and an "error condition" enumeration? (And, if so, why isn't std::is_error_code_enum_v<std::errc> true?)

Is it a special case for some other reason?

Are user-defined error conditions enums supposed to also offer make_error_code() like std::errc does, or just make_error_condition()?
<p>I understand the difference between <code>std::error_code</code> and <code>std::error_condition</code>, and I understand that <code>std::errc</code> is an "error condition" enum and not an "error code" enum. I understand how to compare resulting <code>std::error_code</code> values against "error conditions" and so on. I have read <a href="http://blog.think-async.com/2010/04/system-error-support-in-c0x-part-5.html" rel="nofollow noreferrer">Chris Kohlhoff's blog post about these types</a> several times over the years. I have used these types and extensions to them in production code.</p>
<p>However, I do not understand why <code>std::make_error_code(std::errc)</code> exists.</p>
<p>It doesn't seem to be necessary for <em>comparing</em> actual <code>std::error_code</code> values against <code>std::error_condition</code> values: there is <code>std::make_error_condition(std::errc)</code> and conversions to <code>std::error_condition</code> and special comparison overloads for all of that.</p>
<p>The function's existence is especially puzzling because <code>std::is_error_code_enum_v<std::err></code> is <code>false</code> for <code>std::errc</code>. Presumably that is to <em>prevent</em> implicitly converting <code>std::errc</code> to <code>std::error_code</code> via <code>std::make_error_code(std::errc)</code>, but it isn't clear to me <em>why</em> preventing that is desirable when <code>std::make_error_code(std::errc)</code> exists. In other words, if you shouldn't make an <code>std::error_code</code> out of <code>std::errc</code>, why is there a function that does just that? And if you should, why is that implicit constructor disabled?</p>
<p>Is it <code>std::errc</code> a special case because code often wants to actually produce a real <code>std::error_code</code> in the <code>std::generic_category()</code> with an <code>std::errc</code> value? In other words, is <code>std::errc</code> in some ways <em>both</em> an "error code" and an "error condition" enumeration? (And, if so, why isn't <code>std::is_error_code_enum_v<std::errc></code> <code>true</code>?)</p>
<p>Is it a special case for some other reason?</p>
<p>Are user-defined error conditions enums supposed to also offer <code>make_error_code()</code> like <code>std::errc</code> does, or just <code>make_error_condition()</code>?</p>
I have found how to fix this.
 
Top