boost program-options uses deprecated feature
Asked Answered
P

1

7

I have boost-program-options version 1.78 installed via vcpkg. When I compile with clang++ and -std=c++20 I get the following errors. This doesn't happen when I compile with g++. According to this this std::unary_function is deprecated as of C++11.

In file included from /home/david/C/vcpkg/installed/x64-linux/include/boost/program_options/variables_map.hpp:12:
In file included from /home/david/C/vcpkg/installed/x64-linux/include/boost/any.hpp:20:
In file included from /home/david/C/vcpkg/installed/x64-linux/include/boost/type_index.hpp:29:
In file included from /home/david/C/vcpkg/installed/x64-linux/include/boost/type_index/stl_type_index.hpp:47:
/home/david/C/vcpkg/installed/x64-linux/include/boost/container_hash/hash.hpp:132:33: warning: 'unary_function<const std::error_category *, unsigned long>' is deprecated [-Wdeprecated-declarations]
        struct hash_base : std::unary_function<T, std::size_t> {};
                                ^
/home/david/C/vcpkg/installed/x64-linux/include/boost/container_hash/hash.hpp:692:18: note: in instantiation of template class 'boost::hash_detail::hash_base<const std::error_category *>' requested here
        : public boost::hash_detail::hash_base<T*>
                 ^
/home/david/C/vcpkg/installed/x64-linux/include/boost/container_hash/hash.hpp:420:24: note: in instantiation of template class 'boost::hash<const std::error_category *>' requested here
        boost::hash<T> hasher;
                       ^
/home/david/C/vcpkg/installed/x64-linux/include/boost/container_hash/hash.hpp:551:9: note: in instantiation of function template specialization 'boost::hash_combine<const std::error_category *>' requested here
        hash_combine(seed, &v.category());
        ^
/bin/../lib/gcc/x86_64-redhat-linux/12/../../../../include/c++/12/bits/stl_function.h:124:7: note: 'unary_function<const std::error_category *, unsigned long>' has been explicitly marked deprecated here
    } _GLIBCXX11_DEPRECATED;
      ^
/bin/../lib/gcc/x86_64-redhat-linux/12/../../../../include/c++/12/x86_64-redhat-linux/bits/c++config.h:2340:32: note: expanded from macro '_GLIBCXX11_DEPRECATED'
# define _GLIBCXX11_DEPRECATED _GLIBCXX_DEPRECATED
                               ^
/bin/../lib/gcc/x86_64-redhat-linux/12/../../../../include/c++/12/x86_64-redhat-linux/bits/c++config.h:2331:46: note: expanded from macro '_GLIBCXX_DEPRECATED'
# define _GLIBCXX_DEPRECATED __attribute__ ((__deprecated__))

Why is boost using deprecated parts of the standard? Is there anything wrong with ignoring these warnings and suppressing with -Wno-deprecated-declarations?

Printery answered 15/8, 2022 at 15:43 Comment(1)
Boost doesn't target just the most recent C++ standard, it also supports older C++ standards (although not all Boost component library support all C++ standards, e.g., Signals2.) The current version of Boost is 1.80, which possibly has better support in the component library you are using.Millepore
O
6

The use of std::unary_function has been replaced for compilers/standard libraries not supporting it anymore since Boost 1.64 for MSVC (commit) and since 1.73 for other compilers (commit).

But it continued using std::unary_function as default as long as it was not detected as removed.

Since Boost 1.80 the use of std::unary_function is disabled when using C++11 or later with libstdc++ (commit, issue) to get rid of deprecation warnings and a similar patch has been merged for libc++ (commit, issue). The latter seems to not be included in the 1.80 release, but included in 1.81.

So either you wait/upgrade to a recent version of Boost, add the patch manually or configure the inclusion of the headers as system headers, so that warnings for them are generally suppressed. (I am not sure whether this is completely possible in your setup.) Alternatively disable the deprecation warning with the flag you are showing, but that seems heavy-handed.

Compiling in C++17 mode or later where the function has been completely removed might also help (haven't tested).

Simply defining the macro BOOST_NO_CXX98_FUNCTION_BASE before including any boost header might also help, but I can't tell whether it is intended for a user to do that or whether it might break other boost stuff. It may also silently break ABI for all I know.

Osbourne answered 15/8, 2022 at 16:46 Comment(3)
i guess it's best to just live with the warning until the vcpkg repo is updated. thanks!Printery
github says the libc++ disabling is in on the 1.83.0 tag, so not in 1.80.0Nena
@Nena I wrote that it isn't included in 1.80 which was the latest release at the time of writing. The commit also has the 1.81 tag, so it should be in that release. I adjusted the answer accordingly.Osbourne

© 2022 - 2024 — McMap. All rights reserved.