1

Boost 的program_options库现在支持boost::optional,同样可以用std::optional?

我试图修改文档示例和 PR 中的代码,但似乎都不起作用。

例如,整数的非常简单的情况(在尝试模板特化之前):

void validate(boost::any& v, const std::vector<std::string>& values, std::optional<int>* target_type,
              int) {
  using namespace boost::program_options;
  validators::check_first_occurrence(v);
  const string& s = validators::get_single_string(values);

  int n = lexical_cast<int>(s);
  v = any(std::make_optional<int>(n));
}

失败并出现目标类型无法执行的错误istream

external/boost/boost/lexical_cast/detail/converter_lexical.hpp:243:13: 
error: static_assert failed due to requirement 
'has_right_shift<std::__1::basic_istream<char>, std::__1::optional<int>, boost::binary_op_detail::dont_care>::value || boost::has_right_shift<std::__1::basic_istream<wchar_t>, std::__1::optional<int>, boost::binary_op_detail::dont_care>::value'
"Target type is neither std::istream`able nor std::wistream`able"
4

1 回答 1

2

诸如validateoperator>>以及)之类的问题通常是ADL ¹。

您需要在关联的命名空间之一中声明重载。在这种情况下,因为int是原始类型,所以唯一关联的命名空间来自库代码:

  • std对于optional, vector, string, allocator, char_traits(是的,这些都算!)
  • boost为了any

您不希望在这些命名空间中添加代码,因为当库实现细节发生变化时,您可能会干扰库函数或招致未来的破坏。

如果必须选择,您更愿意选择boost这里,因为

  • 那是提供手头功能的库
  • 自由validate功能被明确设计为定制点

旁注:留意tag_invoke- 在库中构建自定义点的更好方法


修复

说了这么多话,解决方法很简单:

namespace boost::{
    void validate(boost::any& v, const std::vector<std::string>& values,
                  std::optional<int>*, int) {
        using namespace boost::program_options;
        validators::check_first_occurrence(v);
        const std::string& s = validators::get_single_string(values);

        int n = boost::lexical_cast<int>(s);
        v     = boost::any(std::make_optional<int>(n));
    }
} // namespace boost

添加两行使其工作:Live On Wandbox

其他注意事项:

  1. 通常注入的“溶液”operator>>不太纯,因为

    • 它有可能用可能干扰的 ADL 可见重载“感染”所有其他代码。operator>>比 boost 的validate函数使用更多的代码
    • 因此,当另一个翻译单元可能合法地为相同的论点定义另一个翻译单元时,它会由于 ODR违规而引起UB 。operator>>
  2. 在最近的编译器上,你可以说vm.contains而不是稍微滥用vm.count

  3. 还有一个非流式类型的障碍,如果你定义一个默认值,你可能还需要用它指定字符串表示

清单

在 Compiler Explorer 上编译

#include <boost/program_options.hpp>
#include <optional>
#include <iostream>

namespace po = boost::program_options;

namespace boost {
    void validate(boost::any& v, const std::vector<std::string>& values,
                std::optional<int>*, int) {
        using namespace boost::program_options;
        validators::check_first_occurrence(v);
        const std::string& s = validators::get_single_string(values);

        int n = boost::lexical_cast<int>(s);
        v     = boost::any(std::make_optional<int>(n));
    }
} // namespace boost

int main(int ac, char* av[]) {
    try {
        using Value = std::optional<int>;

        po::options_description desc("Allowed options");
        desc.add_options()
            ("help", "produce help message")
            ("value", po::value<Value>()->default_value(10, "10"),
                "value")
        ;

        po::variables_map vm;
        po::store(po::parse_command_line(ac, av, desc), vm);
        po::notify(vm);

        if (vm.contains("value")) {
            std::cout << "value is " << vm["value"].as<Value>().value() << "\n";
        }

    } catch (std::exception& e) {
        std::cout << e.what() << "\n";
        return 1;
    }
}

奖金

作为一个附加练习,让我们演示一下,如果您的可选 value_type不是原始类型,而是您的库类型,在 namespace 中声明MyLib,那么我们没有上面的大部分权衡:

namespace MyLib {
    template <typename T> struct MyValue {
        MyValue(T v = {}) : value(std::move(v)) {}

      private:
        T value;
        friend std::istream& operator>>(std::istream& is, MyValue& mv) {
            return is >> mv.value;
        }
        friend std::ostream& operator<<(std::ostream& os, MyValue const& mv) {
            return os << mv.value;
        }
    };

现在,您可以为 MyLib 命名空间中的任何类型(无论是否可选)提供通用验证器,并让 ADL 通过您的MyLib命名空间找到它们:

    template <typename T, typename Values>
    void validate(boost::any& v, Values const& values, T*, int) {
        po::validators::check_first_occurrence(v);
        v = boost::lexical_cast<T>(
                po::validators::get_single_string(values));
    }

    template <typename T, typename Values>
    void validate(boost::any& v, Values const& values, std::optional<T>*, int) {
        po::validators::check_first_occurrence(v);
        v = std::make_optional(
                boost::lexical_cast<T>(
                    po::validators::get_single_string(values)));
    }
} // namespace MyLib

观看现场演示

#include <boost/program_options.hpp>
#include <iostream>
#include <iomanip>

namespace po = boost::program_options;

namespace MyLib {
    template <typename T> struct MyValue {
        MyValue(T v = {}) : value(std::move(v)) {}

      private:
        T value;
        friend std::istream& operator>>(std::istream& is, MyValue& mv) {
            return is >> std::boolalpha >> mv.value;
        }
        friend std::ostream& operator<<(std::ostream& os, MyValue const& mv) {
            return os << std::boolalpha << mv.value;
        }
    };

    // Provide generic validators for any types in your MyLib namespace, be it
    // optional or not
    template <typename T, typename Values>
    void validate(boost::any& v, Values const& values, T*, int) {
        po::validators::check_first_occurrence(v);
        v = boost::lexical_cast<T>(
                po::validators::get_single_string(values));
    }

    template <typename T, typename Values>
    void validate(boost::any& v, Values const& values, std::optional<T>*, int) {
        po::validators::check_first_occurrence(v);
        v = std::make_optional(
                boost::lexical_cast<T>(
                    po::validators::get_single_string(values)));
    }
} // namespace MyLib

int main(int ac, char* av[]) {
    try {
        using Int    = MyLib::MyValue<int>;
        using OptInt = std::optional<MyLib::MyValue<int>>;
        using OptStr = std::optional<MyLib::MyValue<std::string> >;

        po::options_description desc("Allowed options");
        desc.add_options()
            ("ival", po::value<Int>()->default_value(Int{10}),
                  "integer value")
            ("opti", po::value<OptInt>()->default_value(OptInt{}, "(nullopt)"),
                  "optional integer value")
            ("sval", po::value<OptStr>()->default_value(OptStr{"secret"}, "'secret'"),
                  "optional string value")
        ;

        po::variables_map vm;
        po::store(po::parse_command_line(ac, av, desc), vm);
        po::notify(vm);

        std::cout << "Options: " << desc << "\n";

        if (vm.contains("ival")) {
            std::cout << "ival is " << vm["ival"].as<Int>() << "\n";
        }
        if (vm.contains("opti")) {
            if (auto& v = vm["opti"].as<OptInt>())
                std::cout << "opti is " << v.value() << "\n";
            else
                std::cout << "opti is nullopt\n";
        }
        if (vm.contains("sval")) {
            if (auto& v = vm["sval"].as<OptStr>())
                std::cout << "sval is " << v.value() << "\n";
            else
                std::cout << "sval is nullopt\n";
        }
    } catch (std::exception& e) {
        std::cout << e.what() << "\n";
        return 1;
    }
}

对于./a.out --ival=42 --sval=LtUaE打印:

Options: Allowed options:
  --ival arg (=10)        integer value
  --opti arg (=(nullopt)) optional integer value
  --sval arg (='secret')  optional string value

ival is 42
opti is nullopt
sval is LtUaE

¹ 另请参阅为什么 Boost 在“程序选项”中使用全局函数覆盖来实现自定义验证器

于 2021-03-09T14:14:11.993 回答