【问题标题】:boost serialization warning C4308: negative integral constant converted to unsigned typeboost 序列化警告 C4308:负整数常量转换为无符号类型
【发布时间】:2013-05-20 18:34:07
【问题描述】:

我的结构:

  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>        ]

【问题讨论】:

  • 那么,问题是什么?
  • 为什么会收到警告?如何解决?
  • 使用其他编译器可能会因相同原因生成其他警告 - 例如用叮当声是:include/boost/mpl/print.hpp:50:23: warning: division by zero is undefined [-Wdivision-by-zero]

标签: c++ visual-studio-2008 serialization boost


【解决方案1】:

Boost 担心你是 archiving non-const class instances,如果不同的跟踪对象使用相同的地址,可能会导致对象跟踪出现问题。

要删除警告,您可以将对象强制转换为 const:

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

或者您可以简单地使用 & 运算符:

oa & members;

这是这个特殊(和常见)案例中警告的来源。一般来说,这种类型的编译器警告是 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);

【讨论】:

  • 嗯,boost 认为 &amp; 运算符在这种情况下使用更安全的原因是什么?另一种选择(对于某些用例)也许也使用BOOST_CLASS_TRACKING(, boost::serialization::track_never) - 例如从堆栈中序列化对象时。
  • 我已经找到了引用的解释,但仍然不知道该怎么做 - 我希望该评论包括对 &amp; 运算符的引用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-13
  • 2013-12-14
  • 2018-08-30
  • 2015-12-28
相关资源
最近更新 更多