3

boost :: program_optionsを使用すると、名前空間内で宣言されているときに、コンパイルする独自のオプションタイプを取得できません。ただし、名前空間の外では、コンパイルして正常に動作します。

#include <boost/program_options.hpp>
using namespace boost;
using namespace boost::program_options;

struct my_type1 {
    my_type1(int nn) : n(nn) {}
    int n;
};
namespace nm  {
    struct my_type2 {
        my_type2(int nn) : n(nn) {}
        int n;
    };
}

void validate(boost::any& v,
              const std::vector<std::string>& values,
              my_type1*, int)  {
    const std::string& s = validators::get_single_string(values);
    v = any(my_type1(lexical_cast<int>(s)));
}
void validate(boost::any& v,
              const std::vector<std::string>& values,
              nm::my_type2*, int)  {
    const std::string& s = validators::get_single_string(values);
    v = any(nm::my_type2(lexical_cast<int>(s)));
}

int main()  {
    options_description desc("options");
    desc.add_options()
        ("m1", value<my_type1>()    , "")
        ("m2", value<nm::my_type2>(), "")
    ;
    return 0;
}

main()で、オプション'm1'の宣言はコンパイルされますが、'm2'はコンパイルされません...何が欠けていますか?私はgccバージョン4.4.4でboost_1_43_0を使用しています。

4

1 に答える 1

5

検証関数は、構造体と同じ名前空間にある必要があります。次のmy_typeように変更します。

  namespace nm  {
     void validate(boost::any& v,
                const std::vector<std::string>& values,
                  my_type2*, int)  {
      const std::string& s = validators::get_single_string(values);
      v = any(my_type2(lexical_cast<int>(s)));
    }
  }

それは私のためにコンパイルされます。

于 2010-07-24T09:07:19.120 に答える