gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:59:07 30 4
gpt4 key购买 nike

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

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

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

#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

关于c++ - 将 Boost 序列化与 -Wall -Wextra 一起使用时的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67309934/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com