I have an enumeration class and a corresponding input stream operator in the namespace fw::example.
#include <stdexcept>
#include <string>
#include <istream>
namespace fw {
namespace example {
enum class Booleans {
  kFalse = 0,
  kTrue = 1
};
std::istream& operator>>(std::istream& is, Booleans& boolean) {
  std::string token;
  is >> token;
  if ("true" == token) {
    boolean = Booleans::kTrue;
  } else if ("false" == token) {
    boolean = Booleans::kFalse;
  } else {
    throw std::invalid_argument{
        "Invalid string representation for an enumerator of the Booleans class."};
  }
  return is;
}
}  // namespace example
}  // namespace fw
Then I bind a variable of that enum via Boost.Program_options in the same namespace:
// [...]
description_.add_options()(
"bool",
boost::program_options::value<Booleans>(&boolean_)
    ->required()
    ->default_value(Booleans::kFalse),
"A boolean string, either true or false.");
// [...]
But I do not want to expose the std::runtime_error to the user, instead I want
to use the proper exception from the framework, and that is boost::program_options::invalid_option_value.
Imagine a more complex scenario in which the enum class and the input stream operator are defined in a library and I am unable to modify that library.
So I tried the following (according to an answer I found on SO, which I can not find again):
namespace boost {
namespace program_options {
std::istream& operator>>(std::istream& is,
                         fw::example::Booleans& boolean) {
  try {
    return fw::example::operator>>(is, boolean);
  } catch (std::invalid_argument const& kException) {
    throw invalid_option_value{kException.what()};
  }
}
}  // namespace program_options
}  // namespace boost
The whole code compiles, but the thing is, that only the free function fw::example::operator>> is called and therefore the wrong execption is propagated:
terminate called after throwing an instance of 'std::invalid_argument'
  what():  Invalid string representation for an enumerator of the Booleans class.
I am looking for a solution to be able to cleanly separate the Boost.Program_options related code from the rest, but also use the appropriate exception.
I am using the following environment:
- Boost v1.49
- GCC v4.71 with the -std=c++11argument.
 
     
    