提升程序选项对价值
Boost program options pair value
我观察到 STL 类的 boost::program_options::value
函数的奇怪行为。我经常需要成对地为程序提供参数,例如带有短标签的文件名,但boost::program_options::value
函数似乎不适用于std::pair
,而它确实适用于我自己定义的任何类。请考虑以下代码:
#include <string>
#include <utility>
#include <boost/program_options.hpp>
using namespace std;
namespace po = boost::program_options;
class sspair: public pair<string,string> { };
typedef pair<string,string> mypair;
// typedef sspair mypair;
istream& operator>>(istream& in, mypair& ss) {
string s;
in >> s;
const size_t sep = s.find(':');
if (sep==string::npos) {
ss.first = string();
ss.second = s;
} else {
ss.first = s.substr(0,sep);
ss.second = s.substr(sep+1);
}
return in;
}
int main(int argc, char **argv)
{
mypair a;
try {
po::options_description all_opt("Options");
all_opt.add_options()
("arg,a", po::value<mypair>(&a),"colon separated pair")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, all_opt), vm);
po::notify(vm);
} catch(exception& e) {
cerr << e.what() << endl;
exit(1);
}
cout << "a = (" << a.first << ", " << a.second << ")" << endl;
return 0;
}
有了typedef sspair mypair
我得到了预期的行为。
$ ./test
a = (, )
$ ./test -a b:c
a = (b, c)
$ ./test -a bc
a = (, bc)
但是有了typedef pair<string,string> mypair
我得到以下编译错误:
In file included from /usr/include/boost/any.hpp:27:0,
from /usr/include/boost/program_options/value_semantic.hpp:12,
from /usr/include/boost/program_options/options_description.hpp:13,
from /usr/include/boost/program_options.hpp:15,
from test.cc:4:
/usr/include/boost/lexical_cast.hpp: In instantiation of ‘struct boost::detail::deduce_target_char_impl<boost::detail::deduce_character_type_later<std::pair<std::basic_string<char>, std::basic_string<char> > > >’:
/usr/include/boost/lexical_cast.hpp:415:89: required from ‘struct boost::detail::deduce_target_char<std::pair<std::basic_string<char>, std::basic_string<char> > >’
/usr/include/boost/lexical_cast.hpp:674:92: required from ‘struct boost::detail::lexical_cast_stream_traits<std::basic_string<char>, std::pair<std::basic_string<char>, std::basic_string<char> > >’
/usr/include/boost/lexical_cast.hpp:2363:19: required from ‘static Target boost::detail::lexical_cast_do_cast<Target, Source>::lexical_cast_impl(const Source&) [with Target = std::pair<std::basic_string<char>, std::basic_string<char> >; Source = std::basic_string<char>]’
/usr/include/boost/lexical_cast.hpp:2543:50: required from ‘Target boost::lexical_cast(const Source&) [with Target = std::pair<std::basic_string<char>, std::basic_string<char> >; Source = std::basic_string<char>]’
/usr/include/boost/program_options/detail/value_semantic.hpp:89:38: required from ‘void boost::program_options::validate(boost::any&, const std::vector<std::basic_string<charT> >&, T*, long int) [with T = std::pair<std::basic_string<char>, std::basic_string<char> >; charT = char]’
/usr/include/boost/program_options/detail/value_semantic.hpp:170:55: required from ‘void boost::program_options::typed_value<T, charT>::xparse(boost::any&, const std::vector<std::basic_string<charT> >&) const [with T = std::pair<std::basic_string<char>, std::basic_string<char> >; charT = char]’
test.cc:49:1: required from here
/usr/include/boost/lexical_cast.hpp:388:13: error: invalid application of ‘sizeof’ to incomplete type ‘boost::STATIC_ASSERTION_FAILURE<false>’
BOOST_STATIC_ASSERT_MSG((result_t::value || boost::has_right_shift<std::basic_istream<wchar_t>, T >::value),
^
make: *** [test] Error 1
我发现如果我尝试使用其他 stl 容器(如 std::array
或 std::tuple
,也会发生类似的行为。
有人知道问题出在哪里吗?
编辑:
好的,在我阅读这篇文章后,我刚刚发现了导致此问题的原因。 显然,流运算符仅在定义类(po::value 函数的模板参数)的命名空间中查找。所以,通过编辑
namespace std {
istream& operator>>(istream& in, mypair& ss) { ... }
}
pair<string,string>
类直接工作。
现在,在 std 命名空间中定义运算符有什么缺点吗?我听说这本身不符合标准。
抱歉,只有时间快速回答。
如果要使用 std::pair<std::string, std:string>
作为配对,则需要为其编写一个operator>>(...)
,并且此运算符也需要在 namespace std
中,以便 ADL 工作。
相关文章:
- LLVM | codegen 用于带有命令行选项的程序输入功能
- 如何在提升程序选项中设置矢量<矢量>的默认值<string>
- Bash 自动完成一个选项,而不运行应用程序
- 由于选项卡顺序的原因,鼠标光标总是得到错误的hwnd-MFC应用程序
- 如何创建一个程序来替换单个空白/选项卡的多个空白/选项卡
- C++ 使用 json 文件提升程序选项
- 如何在 DirectX 11 应用程序中提供选择图形适配器的选项?
- 提升程序选项:链接错误
- 相机枚举的选项和UWP应用程序的HAL(WPF/C CLI DLL)
- 对程序使用 switch 语句,导致选项的无限循环
- 如果应用程序从安装选项启动,则加载库失败,错误代码为 126
- 提升程序选项的多个选项值
- 在任务管理器的应用程序选项卡中获取运行任务的列表
- 获取提升程序选项以在函数范围之后保留
- 无法弄清楚如何为用户提供启动,停止和重新启动程序的所有选项?
- boost::any_cast 使用程序选项 C++ 抛出
- clang-format用于提升程序选项
- 提升程序选项:参数被多次检测到
- /gs选项导致程序引发异常
- 在C#WPF应用程序中使用C++11库的选项是什么