3

I'm trying to use Boost.serialize. It appears to work fine, but I'm getting warnings when I turn on -Wextra and -Wall warnings with g++ (Version 10.2.0).

It ran fine, and compiled fine when I switch off the warnings. However I'd like to be able to get rid of them properly. I've been trying for hours but cannot find a solution. I found some examples of similar warnings, but those were cases when default constructors were not defined.

I have created a minimal example below:

#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;
}

The output when I compile is

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){
      |                               ~~~~~~~~~~~~~~~^~~~
'''
iFreilicht
  • 13,271
  • 9
  • 43
  • 74
TCD
  • 151
  • 1
  • 7

1 Answers1

2

It's easy to fix, just remove the unused argument's name, or comment out its name.

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

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

    int x;
};

With C++17 you have [[maybe_unused]] attribute specifier, like:

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

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

    int x;
};

See this question for further reading:

Unused parameter in c++11

prehistoricpenguin
  • 6,130
  • 3
  • 25
  • 42
  • Yes. That works perfectly! Thank you very much. I hadn't realised that all the lines at the top were being caused by the simple "regular" warning at the end. – TCD Apr 29 '21 at 08:26
  • @TCD :) The compiler error of boost is horrible, which makes people scared – prehistoricpenguin Apr 29 '21 at 08:57
  • I make it a point to also reduce the useless top-level const. We need to teach people not to cargo cult and to know what compilers see anyways. In C==20 we can finally write `void serialize(auto& ar, unsigned){}` :) – sehe Apr 29 '21 at 21:50
  • @sehe, can you clarify your comment? Is it a comment about C++20's `auto` argument or about spurious `const` in function definition? – alfC Apr 30 '21 at 02:18
  • @sehe Thank you for point out it. Understanding what compilers see is hard, which makes c++ expert-oriented. – prehistoricpenguin Apr 30 '21 at 02:19
  • 3
    In my opinion `[[maybe_unused]]` is not for this. `ver` is `[[definitely_unused]]` :). A commented argument name seems a better solution. – alfC Apr 30 '21 at 02:20
  • 1
    @alfC I agree with you. I will update the answer. – prehistoricpenguin Apr 30 '21 at 02:22
  • 1
    @alfC both, I hate that libraries present example code with the same meaningless red tape every time to the extent that casual users of the library will be copying that around the global thousands of times because "maybe its important". [Similar with other cargo cult areas like class vs struct, getters/setters, unnecessary special members etc.] Incidentally, this is in large part what I dislike most about Spirit X3 documentation. It's just a lot of hoop jumping, but the real hoops are invisible. – sehe Apr 30 '21 at 09:49
  • @sehe I think it's caused by lacking contributors, writing good document is time-consuming and boring.Even the open-source code from google and Facebook has the problem of not meticulous documentation – prehistoricpenguin Apr 30 '21 at 10:06
  • @sehe , do you mean the top-level const being useless here because the parameter is not actually used in this version of the function? Conceptually though, would it not indicate to a maintainer, that if they need to explicitly use it later in a modified version of the function, that it should be const then? – TCD Apr 30 '21 at 10:17
  • @TCD It "should" not be const. There's no difference in prototype between `int foo(int)` or `int const foo(int const)`. Of course, **if** you happen to be using the parameter you **may** mark it const so it cannot (easily) be altered in the body of the function. [IMO There's no need to include such convoluted motives in the documentation of the library interface, since it literally cannot matter.] It's a bit of a pet peeve, I can count [many hundreds of times](https://stackoverflow.com/search?q=user%3A85371+boost+serialization+serialize) where I've seen stuff like this mindlessly replicated. – sehe Apr 30 '21 at 12:31