JACoders / OpenJK

Community effort to maintain and improve Jedi Academy (SP & MP) + Jedi Outcast (SP only) released by Raven Software
GNU General Public License v2.0
2.03k stars 614 forks source link

JK2 fails to build with gcc 8: std::map with wrongly-typed std::allocator #985

Closed smcv closed 6 years ago

smcv commented 6 years ago

Operating system and version:

Debian unstable (development rolling release branch) with gcc-8 8.2.0-3

Is this for single player or multiplayer?

JK2 single player

Description of the bug (and if possible, steps to reproduce the bug):

Compile with JK2 enabled. Compilation fails with several errors similar to this:

In file included from /usr/include/c++/8/map:61,
                 from /home/smcv/src/openjk/codeJK2/game/../game/../../code/ui/../qcommon/../game/ghoul2_shared.h:28,
                 from /home/smcv/src/openjk/codeJK2/game/../game/../../code/ui/../qcommon/q_shared.h:512,
                 from /home/smcv/src/openjk/codeJK2/game/../game/../../code/ui/gameinfo.h:27,
                 from /home/smcv/src/openjk/codeJK2/game/../game/g_local.h:32,
                 from /home/smcv/src/openjk/codeJK2/game/g_headers.h:28,
                 from /home/smcv/src/openjk/codeJK2/game/g_ICARUS.cpp:25:
/usr/include/c++/8/bits/stl_map.h: In instantiation of ‘class std::map<std::__cxx11::basic_string<char>, pscript_s*, std::less<std::__cxx11::basic_string<char> >, std::allocator<pscript_s*> >’:
/home/smcv/src/openjk/codeJK2/game/g_ICARUS.cpp:34:15:   required from here
/usr/include/c++/8/bits/stl_map.h:122:21: error: static assertion failed: std::map must have the same value_type as its allocator
       static_assert(is_same<typename _Alloc::value_type, value_type>::value,
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/8/bits/stl_map.h: In instantiation of ‘class std::map<std::__cxx11::basic_string<char>, int, std::less<std::__cxx11::basic_string<char> >, std::allocator<int> >’:
/home/smcv/src/openjk/codeJK2/game/g_ICARUS.cpp:35:13:   required from here
/usr/include/c++/8/bits/stl_map.h:122:21: error: static assertion failed: std::map must have the same value_type as its allocator

What did you expect to happen instead?

Successful compilation

Diagnosis

Several std::map-based typedefs in JK2 are of the form std::map <K, V, std::less<K>, std::allocator<V>> for some key K and value V. However, std::map expects its 4th parameter (the allocator) to be an Allocator that is to be used to allocate instances of std::pair<const K, V>, not instances of V; so it should probably be std::allocator<std::pair<const K, V>> instead.

std::allocator<std::pair<const K, V>> is the default allocator anyway, so this template parameter can simply be omitted. That compiles successfully: I'll send a pull request when I've tested the resulting binaries.

Similarly, the default comparator (3rd template parameter) is std::less<K>, so it could probably also be omitted, although this is not necessary to fix the compilation failure.

smcv commented 6 years ago

After fixing #987, the macOS build has the same failure:

In file included from /Users/travis/build/smcv/OpenJK/codeJK2/game/Q3_Interface.cpp:30:
In file included from /Users/travis/build/smcv/OpenJK/codeJK2/game/g_headers.h:28:
In file included from /Users/travis/build/smcv/OpenJK/codeJK2/game/g_local.h:32:
In file included from /Users/travis/build/smcv/OpenJK/codeJK2/game/../../code/ui/gameinfo.h:27:
In file included from /Users/travis/build/smcv/OpenJK/codeJK2/game/../cgame/../../code/qcommon/q_shared.h:512:
In file included from /Users/travis/build/smcv/OpenJK/codeJK2/game/../../code/ui/../qcommon/../game/../rd-common/../game/ghoul2_shared.h:28:
/Applications/Xcode-9.4.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/map:818:5: error: static_assert failed "Allocator::value_type must be same type as value_type"
    static_assert((is_same<typename allocator_type::value_type, value_type>::value),
    ^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/travis/build/smcv/OpenJK/codeJK2/game/Q3_Interface.cpp:915:2: note: in instantiation of template class 'std::__1::map<std::__1::basic_string<char>, int, std::__1::less<std::__1::basic_string<char> >, std::__1::allocator<int> >' requested here
        entlist_t::iterator             ei;
        ^