2013-05-25 6 views
9

私のstruct:ブーストシリアル化の警告C4308:負の整数定数は、符号なしの型に変換

struct member{ 
     std::string ip_address; 
     std::string port; 

    protected: 
     friend class boost::serialization::access; 

     template<class Archive> 
     void serialize(Archive & ar, const unsigned int version) 
     { 
      ar & ip_address; 
      ar & port; 
     } 
    }; 

私は保存し、それが私はそれがあることを期待して、すべてのデータがあり、完璧に動作ロードするためにそれを使用し、

std::vector<member> members; 
std::ostringstream ss; 
boost::archive::text_oarchive oa(ss); 
oa<<members; 


std::istringstream ss_(received_data.data()); 
boost::archive::text_iarchive ia(ss_); 
ia>>members; 

が、私はこの警告

warning C4308: negative integral constant converted to unsigned type 
1>  c:\program files\boost\boost_1_51\boost\serialization\static_warning.hpp(92) : see reference to class template instantiation 'boost::mpl::print<T>' being compiled 
1>  with 
1>  [ 
1>   T=boost::serialization::BOOST_SERIALIZATION_STATIC_WARNING_LINE<98> 
1>  ] 
1>  c:\program files\boost\boost_1_51\boost\archive\detail\check.hpp(98) : see reference to class template instantiation 'boost::serialization::static_warning_test<B,L>' being compiled 
1>  with 
1>  [ 
1>   B=false, 
1>   L=98 
1>  ] 
1>  c:\program files\boost\boost_1_51\boost\archive\detail\oserializer.hpp(313) : see reference to function template instantiation 'void boost::archive::detail::check_object_tracking<T>(void)' being compiled 
1>  with 
1>  [ 
1>   T=std::vector<member> 
1>  ] 
1>  c:\program files\boost\boost_1_51\boost\archive\detail\oserializer.hpp(525) : see reference to function template instantiation 'void boost::archive::detail::save_non_pointer_type<Archive>::invoke<T>(Archive &,T &)' being compiled 
1>  with 
1>  [ 
1>   Archive=boost::archive::text_oarchive, 
1>   T=std::vector<member> 
1>  ] 
1>  c:\program files\boost\boost_1_51\boost\archive\detail\common_oarchive.hpp(69) : see reference to function template instantiation 'void boost::archive::save<Archive,T>(Archive &,T &)' being compiled 
1>  with 
1>  [ 
1>   Archive=boost::archive::text_oarchive, 
1>   T=std::vector<member> 
1>  ] 
1>  c:\program files\boost\boost_1_51\boost\archive\basic_text_oarchive.hpp(80) : see reference to function template instantiation 'void boost::archive::detail::common_oarchive<Archive>::save_override<T>(T &,int)' being compiled 
1>  with 
1>  [ 
1>   Archive=boost::archive::text_oarchive, 
1>   T=std::vector<member> 
1>  ] 
1>  c:\program files\boost\boost_1_51\boost\archive\detail\interface_oarchive.hpp(63) : see reference to function template instantiation 'void boost::archive::basic_text_oarchive<Archive>::save_override<T>(T &,int)' being compiled 
1>  with 
1>  [ 
1>   Archive=boost::archive::text_oarchive, 
1>   T=std::vector<member> 
1>  ] 
1>  c:\users\user\desktop\shve\shve\member_server.h(58) : see reference to function template instantiation 'Archive &boost::archive::detail::interface_oarchive<Archive>::operator <<<std::vector<_Ty>>(T &)' being compiled 
1>  with 
1>  [ 
1>   Archive=boost::archive::text_oarchive, 
1>   _Ty=member, 
1>   T=std::vector<member> 
1>  ] 
+0

これは何ですか? –

+0

なぜ警告が表示されるのですか?解決方法を教えてください。 –

+0

他のコンパイラでは、同じ原因に対して他の警告が生成される可能性があります。 include/boost/mpl/print.hpp:50:23:警告:0による除算は定義されていません[-Wdivision-by-zero] ' – maxschlepzig

答えて

16

ブーストは、あなたがarchiving non-const class instancesであることを神経質になってしまうのコンパイルには、異なる追跡対象オブジェクトが同じアドレスを使用する場合、オブジェクト追跡に問題を引き起こします。

oa << const_cast<const std::vector<member>&>(members); 

それとも、単に&演算子を使用することができます:(

oa & members; 

この特定に警告の源だったあなたがconstのにあなたのオブジェクトをキャストすることができ、警告を削除するには

一般的なケース)。一般に、このタイプのコンパイラ警告は、Boostによって目的のためにBOOST_STATIC_WARNINGマクロを呼び出すことで生成されます。そのため、Boostがあなたに慎重を期して欲しいと思っているものが原因です。これは一般に、マクロ呼び出しに伴うコメント(コンパイラのエラーメッセージから見つけることができます)に記述されています。たとえば、この特定の警告の呼び出しは、boost\archive\detail\check.hppからです:

// saving an non-const object of a type not marked "track_never) 
// may be an indicator of an error usage of the 
// serialization library and should be double checked. 
// See documentation on object tracking. Also, see the 
// "rationale" section of the documenation 
// for motivation for this checking. 

BOOST_STATIC_WARNING(typex::value); 
+0

Hm、boostが' '演算子はそのコンテキストで使う方が安全ですか?代わりに(いくつかのユースケースの場合)、おそらく 'BOOST_CLASS_TRACKING(boost :: serialization :: track_never)'を使うこともあります。スタックからオブジェクトを直列化するとき。 – maxschlepzig

+0

私は参照された説明を追跡しましたが、やはり何をすべきか分かりませんでした。そのコメントに '&'演算子への参照が含まれていれば幸いです。 – sage

関連する問題