Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GCC: Fix noexcept warnings on hash functions #1113

Merged
merged 1 commit into from
Oct 24, 2022

Conversation

genpfault
Copy link

@genpfault genpfault commented Jul 25, 2022

Fixes warnings with the hash functions when building with -Wnoexcept on GCC:

// g++ -Wsystem-headers -Wnoexcept main.cpp
#include "glm/glm.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include "glm/gtx/hash.hpp"

#include <unordered_map>

int main()
{
    std::unordered_map<glm::ivec2, int> foo;
    foo[glm::ivec2(0, 0)]++;
    
    return 0;
}

Output:

In file included from glm/detail/setup.hpp:701,
                 from glm/glm.hpp:105,
                 from main.cpp:2:
/usr/include/c++/10/type_traits:2915:14: warning: noexcept-expression evaluates to ‘false’ because of a call to ‘std::size_t std::hash<glm::vec<2, T, Q> >::operator()(const glm::vec<2, T, Q>&) const [with T = int; glm::qualifier Q = glm::packed_highp; std::size_t = long unsigned int]’ [-Wnoexcept]
 2915 |       return noexcept(std::declval<_Fn>()(std::declval<_Args>()...));
      |              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from glm/gtx/hash.hpp:142,
                 from main.cpp:4:
glm/gtx/hash.inl:32:28: note: but ‘std::size_t std::hash<glm::vec<2, T, Q> >::operator()(const glm::vec<2, T, Q>&) const [with T = int; glm::qualifier Q = glm::packed_highp; std::size_t = long unsigned int]’ does not throw; perhaps it should be declared ‘noexcept’
   32 |  GLM_FUNC_QUALIFIER size_t hash<glm::vec<2, T, Q>>::operator()(glm::vec<2, T, Q> const& v) const
      |     

GCC version:

$ g++ --version
g++ (Debian 10.2.1-6) 10.2.1 20210110

@genpfault
Copy link
Author

StoleBorrowed the GLM_NOEXCEPT idea from JesseTG's 2015 PR.

Fix warnings on hash functions with GCC and -Wnoexcept:
* Add GLM_HAS_NOEXCEPT flag & GLM_NOEXCEPT #define to setup.hpp.
* Add GLM_NOEXCEPT to hash functions in hash.hpp.
* Add GLM_NOEXCEPT to matrix operator[] accessors.
* Add gtx_hash.cpp and a test to verify all hash overloads compile.
  Configure with -DCMAKE_CXX_FLAGS="-Werror -Wnoexcept" to test.
Copy link

@christophe-lunarg christophe-lunarg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a very reasonable change with tests, thanks !

@christophe-lunarg christophe-lunarg merged commit 8339c77 into g-truc:master Oct 24, 2022
@genpfault genpfault deleted the fix-noexcept-warnings branch October 25, 2022 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants