boost::program_options中的类型std::optional将无法编译。

huangapple go评论60阅读模式
英文:

boost::program_options option of type std::optional<string> will not compile

问题

尝试添加类型为 std::optional<string> 的 add_options() 行导致编译错误。boost::program_options 不支持这种类型吗?如何在 boost::program_options 中管理可选选项?

#include <iostream>
#include <optional>
using namespace std;
#include <boost/program_options.hpp>
using namespace boost;

int main(int argc, const char* argv[])
{
     cout << "Hello World\n" << boolalpha;

     program_options::options_description options_description("options");
     options_description.add_options()
          ("help", "请帮助我使这段代码能够无错误编译。下面的行需要被注释掉以进行干净的编译。boost 不允许 options 的 optional<> 类型吗?感谢您的帮助")
          ("optional string", program_options::value<std::optional<string>>(), "这个选项是可选的")
          ;
}

期望干净的编译并且希望 boost::program_options 支持 std::optional<> 类型。

英文:

The line attempting to add_options() of type std::optional&lt;string&gt; results in a compile error. does boost::program_options not support this type? how are optional options managed w/ boost::program_options?

#include &lt;iostream&gt;
#include &lt;optional&gt;
using namespace std;
#include &lt;boost\program_options.hpp&gt;
using namespace boost;

int main(int argc, const char* argv[])
{
     cout &lt;&lt; &quot;Hello World\n&quot; &lt;&lt; boolalpha;

     program_options::options_description options_description(&quot;options&quot;);
     options_description.add_options()
          (&quot;help&quot;, &quot;please help me get this code to compile w/o error . the line below needs to be commented out for a clean compile . does boost not permit an options optional&lt;&gt; type ? Thank You Kindly&quot;)
          (&quot;optional string&quot;, program_options::value&lt;std::optional&lt;string&gt;&gt;, &quot;this option is optional&quot;)
          ;
}

expected clean compile and boost::program_options to support std::optional<> type

答案1

得分: 1

你忘了调用 value<>()

此外,可选选项可以表示为:

  • variable_map 中没有存储值
  • 或者存储在 variable_map 中的默认值(来自 option_description

我在类似的情况下解释了背景信息:https://stackoverflow.com/questions/74187581/using-boostoptional-with-multitoken-and-or-composing/74194810#74194810

当然,你可以坚持使用 boost::optional,例如,因为你想提供 boost::none 作为默认值(虽然这在故事情节中有些牵强附会)。在这种情况下,我建议使用 boost::optional,因为它受到库支持:

在 Coliru 上查看

#include <iostream>
#include <boost/optional.hpp>
#include <boost/program_options.hpp>
namespace po = boost::program_options;

int main(/*int argc, char const* argv[]*/) {
    std::cout << std::boolalpha;

    po::options_description options_description("options");
    options_description.add_options()
        ("optional string", po::value<boost::optional<std::string>(), "this option is optional")
        ;
}

如果你必须使用 std::optional<> 支持,你将不得不自行添加它:https://stackoverflow.com/questions/66539770/using-boostprogram-options-with-stdoptional/66548554#66548554

英文:

You forgot to call value&lt;&gt;().

Besides, optional options are represented by either

  • no value stored inside the variable_map
  • or the default value (from the option_description) stored inside the variable_map

I explained the background in a similar situation here: https://stackoverflow.com/questions/74187581/using-boostoptional-with-multitoken-and-or-composing/74194810#74194810

Of course, you can insist in your case, e.g. because you want to supply boost::none as the default value (just making things up for the story-line, albeit far-fetched). In that case, I suggest boost::optional as it enjoys library support:

Live On Coliru

#include &lt;iostream&gt;
#include &lt;boost/optional.hpp&gt;
#include &lt;boost/program_options.hpp&gt;
namespace po = boost::program_options;

int main(/*int argc, char const* argv[]*/) {
    std::cout &lt;&lt; std::boolalpha;

    po::options_description options_description(&quot;options&quot;);
    options_description.add_options()                                                             //
        (&quot;optional string&quot;, po::value&lt;boost::optional&lt;std::string&gt;&gt;(), &quot;this option is optional&quot;) //
        ;
}

If you must have std::optional&lt;&gt; support you'll have to add it yourself: https://stackoverflow.com/questions/66539770/using-boostprogram-options-with-stdoptional/66548554#66548554

huangapple
  • 本文由 发表于 2023年2月18日 03:12:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定