将 Boost 序列化与 -Wall -Wextra 一起使用时的警告

Warnings when using Boost Serialization with -Wall -Wextra

我正在尝试使用 Boost.serialize。它似乎工作正常,但当我使用 g++(版本 10.2.0)打开 -Wextra-Wall 警告时,我收到警告。

它 运行 很好,当我关闭警告时编译也很好。但是我希望能够正确地摆脱它们。我已经尝试了几个小时,但找不到解决方案。我发现了一些类似警告的示例,但这些都是未定义默认构造函数的情况。

我在下面创建了一个最小的例子:

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

struct minimal {
    minimal()=default;
    minimal(int in) 
        : x {in}
    {}  

    template <typename Archive>
        void serialize(Archive & ar, const unsigned vers){
            ar & x;
        }   

    int x;
};

int main()
{
    std::stringstream ss; 
    boost::archive::text_oarchive oa {ss};

    minimal M {10} ; 
    oa << M;  

    return 0;
}

我编译时的输出是

g++ -Wextra -Wall minimal.cc  -lboost_serialization 

minimal.cc: In instantiation of 'void minimal::serialize(Archive&, unsigned int) [with Archive = boost::archive::text_oarchive]':
/usr/include/boost/serialization/access.hpp:116:20:   required from 'static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive; T = minimal]'
/usr/include/boost/serialization/serialization.hpp:59:22:   required from 'void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive; T = minimal]'
/usr/include/boost/serialization/serialization.hpp:109:14:   required from 'void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive; T = minimal]'
/usr/include/boost/archive/detail/oserializer.hpp:153:40:   required from 'void boost::archive::detail::oserializer<Archive, T>::save_object_data(boost::archive::detail::basic_oarchive&, const void*) const [with Archive = boost::archive::text_oarchive; T = minimal]'
/usr/include/boost/archive/detail/oserializer.hpp:106:1:   [ skipping 2 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/boost/archive/detail/oserializer.hpp:315:22:   required from 'static void boost::archive::detail::save_non_pointer_type<Archive>::invoke(Archive&, const T&) [with T = minimal; Archive = boost::archive::text_oarchive]'
/usr/include/boost/archive/detail/oserializer.hpp:539:18:   required from 'void boost::archive::save(Archive&, T&) [with Archive = boost::archive::text_oarchive; T = const minimal]'
/usr/include/boost/archive/detail/common_oarchive.hpp:71:22:   required from 'void boost::archive::detail::common_oarchive<Archive>::save_override(T&) [with T = const minimal; Archive = boost::archive::text_oarchive]'
/usr/include/boost/archive/basic_text_oarchive.hpp:83:52:   required from 'void boost::archive::basic_text_oarchive<Archive>::save_override(T&) [with T = const minimal; Archive = boost::archive::text_oarchive]'
/usr/include/boost/archive/detail/interface_oarchive.hpp:70:36:   required from 'Archive& boost::archive::detail::interface_oarchive<Archive>::operator<<(const T&) [with T = minimal; Archive = boost::archive::text_oarchive]'
minimal.cc:24:11:   required from here
minimal.cc:11:46: warning: unused parameter 'vers' [-Wunused-parameter]
   11 |  void serialize(Archive & ar, const unsigned vers){
      |                               ~~~~~~~~~~~~~~~^~~~
'''

修复起来很容易,只需删除未使用的参数名称,或注释掉它的名称即可。

struct minimal {
    minimal()=default;
    minimal(int in) 
        : x {in}
    {}  

    template <typename Archive>
        void serialize(Archive & ar, unsigned /*vers*/){
            ar & x;
        }   

    int x;
};

对于 C++17,你有 [[maybe_unused]] 属性说明符,例如:

struct minimal {
    minimal()=default;
    minimal(int in) 
        : x {in}
    {}  

    template <typename Archive>
        void serialize(Archive & ar,[[maybe_unused]] unsigned ver){
            ar & x;
        }   

    int x;
};

请参阅此问题以进一步阅读:

Unused parameter in c++11