获取 boost::p rogram_options 将输入视为常量字符*而不是 std::string

getting boost::program_options to treat input as const char* instead of std::string

本文关键字:字符 常量 string std rogram boost options 输入 获取      更新时间:2023-10-16
class Foo {};
Foo foo;
namespace po = boost::program_options;
boost::program_options::options_description desc("Allowed options")
desc.add_options()
    ("foo", po::value<Foo>(&foo));
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);

以上最终将尝试从 std::string& 到 Foo & 进行lex_cast

有没有办法让它从 const char*& 到 Foo & 进行lex_cast?

谢谢!

您可以通过

Foo 定义 istream 运算符来处理此问题:

std::istream& operator>>(std::istream &input_stream, Foo &foo) {
    // read from input_stream into foo...
    // if read fails, set failbit in input_stream...
    return input_stream;
}