Constructing error_conditions does not work!!

Hi,

I use MS Visual Studio 2015 and am trying to generate an error condition with value std::errc::invalid_argument. According to the header file system_error, there is a conditional constructor for std::error_condition for cases where the first argument satisfies std::is_error_condition_enum.

When such a trait is satisfied (which is satisfied for std::errc::invalid_argument, then the said constructor should be available and the following code SHOULD compile:

1
2
auto econd = std::errc::invalid_argument;
std::error_condition ec(econd, std::system_category());


BUT, alas, it does not! The declaration for this constructor is:

1
2
template<class _Enum, class = typename enable_if<is_error_condition_enum<_Enum>::value, void>::type>
error_condition::error_condition(_Enum _Errcode){...}


For some reason, this constructor becomes available but there is still a mismatch for the first argument and I believe there is something wrong with this declaration.

I played with a similar case on a class of mine and got it to work with the following constructor:
template<class _Enum, typename std::enable_if<std::is_error_condition_enum<_Enum>::value>::type* = nullptr>

What could be wrong with the provided declaration and IF indeed wrong, HOW can we correct it??? (Modifying the header file seems NOT a good policy since it is a system header...)

Thanks,
Juan
can you show the code you tried?

also, the normal way to make an error condition out of an errc enum is to call make_error_condition, like so:

auto err = std::make_error_condition(std::errc::invalid_argument);

demo:
http://coliru.stacked-crooked.com/a/470188dafd15f9ca for gcc,
http://rextester.com/HUW83517 for MSVC

docs
http://en.cppreference.com/w/cpp/error/errc
http://en.cppreference.com/w/cpp/error/errc/make_error_condition
Last edited on
The code is:

throw std::system_error(std::make_error_condition(std::errc::invalid_argument), "Argument ... is not valid");

and the output error was
No constructor could take the source type, or constructor overload resolution was ambiguous


system_error constructor (http://en.cppreference.com/w/cpp/error/system_error/system_error) takes error_code, not error_condition, so throw std::system_error(std::make_error_code(std::errc::invalid_argument), "Argument ... is not valid"); (although what's wrong with invalid_argument's built-in message?)


Got it! You are absolutely right!!

Thanks!
Topic archived. No new replies allowed.