C++ Boost程序选项对值

C++ Boost程序选项对值,c++,boost,stl,boost-program-options,std-pair,C++,Boost,Stl,Boost Program Options,Std Pair,我正在观察STL类的boost::program\u options::value函数的一个奇怪行为。 我经常需要成对地为程序提供参数,例如带有短标签的文件名,但是boost::program\u options::value函数似乎不适用于std::pair,而它适用于我自己定义的任何类。考虑下面的代码: #include <string> #include <utility> #include <boost/program_options.hpp> u

我正在观察STL类的
boost::program\u options::value
函数的一个奇怪行为。 我经常需要成对地为程序提供参数,例如带有短标签的文件名,但是
boost::program\u 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 pair 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
类直接工作


现在,在std名称空间中定义运算符有什么缺点吗?我听说这不是标准的complient本身。

对不起,我只有时间快速回答

如果要将
std::pair
用作pair,则需要为其编写一个
操作符>>(…)
,并且该操作符还需要位于
命名空间std
中,以便正常工作

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
namespace std {
  istream& operator>>(istream& in, mypair& ss) { ... }
}