Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 2, 2025

The specialization of std::hash for gsl::not_null<std::shared_ptr<T>> was failing to compile due to incorrect type deduction in the not_null_hash template.

Problem

The issue occurred because the template parameter U was being deduced from decltype(std::declval<const T&>().get()), which for gsl::not_null<std::shared_ptr<int>> would resolve to const std::shared_ptr<int>& instead of std::shared_ptr<int>. This caused std::hash<const std::shared_ptr<int>&> to not be default constructible, triggering the deleted constructor specialization.

// This would fail to compile before the fix
std::hash<gsl::not_null<std::shared_ptr<int>>> hasher{};

Solution

Changed the not_null_hash template to use typename T::element_type instead of deducing the type from .get():

// Before
template <class T, class U = decltype(std::declval<const T&>().get()), ...>

// After  
template <class T, class U = typename T::element_type, ...>

This directly uses the element_type member typedef from gsl::not_null<T> (which is defined as using element_type = T;), avoiding const/reference qualifiers that were causing the hash specialization to fail.

Testing

  • Added test case for std::hash<gsl::not_null<std::shared_ptr<T>>> in the existing TestStdHash test
  • Verified the fix works with both C++14 and C++20 builds
  • All existing tests continue to pass (86/86 tests pass)
  • Confirmed compatibility with other pointer types (raw pointers, unique_ptr, etc.)

Fixes #1209.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] Compilation error with std::hash<gsl::not_null<std::shared_ptr<T>>> in GSL 4.2.0 Fix std::hash compilation error for gsl::not_null<std::shared_ptr<T>> Jun 2, 2025
@Copilot Copilot AI requested a review from carsonRadtke June 2, 2025 19:56
Copilot finished work on behalf of carsonRadtke June 2, 2025 19:56
@carsonRadtke carsonRadtke requested a review from Copilot June 2, 2025 20:07
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes the compilation error related to the specialization of std::hash for gsl::not_null<std::shared_ptr> by changing the template deduction from decltype(.get()) to use typename T::element_type.

  • Update not_null_hash template in include/gsl/pointers to correctly deduce the type.
  • Add new tests in tests/notnull_tests.cpp to verify correct behavior of std::hash with not_null.
  • Ensure compatibility with both C++14 and C++20 standards.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
tests/notnull_tests.cpp Added test cases to validate correct hashing behavior for not_null<std::shared_ptr>.
include/gsl/pointers Updated the not_null_hash template to use typename T::element_type for proper type deduction.

@carsonRadtke carsonRadtke marked this pull request as ready for review June 3, 2025 14:35


template <class T, class U = decltype(std::declval<const T&>().get()), bool = std::is_default_constructible<std::hash<U>>::value>
template <class T, class U = typename T::element_type, bool = std::is_default_constructible<std::hash<U>>::value>
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a comment here saying that "T is conceptually a pointer so we don't have to worry about it being a reference and violating std::hash requirements".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the requested comment in commit 87b3085. The comment explains that T is conceptually a pointer so we don't have to worry about it being a reference and violating std::hash requirements.

@Copilot Copilot AI requested a review from carsonRadtke July 26, 2025 03:13
Copilot finished work on behalf of carsonRadtke July 26, 2025 03:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compilation error with std::hash<gsl::not_null<std::shared_ptr<T>>> in GSL 4.2.0
2 participants