Skip to content

Add std::string_view constructor to hashed_string#1334

Open
skhaz wants to merge 1 commit into
skypjack:mainfrom
skhaz:feature/string_view-constructor
Open

Add std::string_view constructor to hashed_string#1334
skhaz wants to merge 1 commit into
skypjack:mainfrom
skhaz:feature/string_view-constructor

Conversation

@skhaz
Copy link
Copy Markdown

@skhaz skhaz commented Apr 22, 2026

Summary

Adds a convenience constructor accepting std::string_view directly, eliminating the need for manual .data() and .size() calls.

Motivation

As noted in PR #824, std::string_view support was mentioned as desirable but not included at the time. This PR addresses that gap.

Many codebases use std::string_view extensively, and users currently write verbose code:

// Before: verbose
const auto key = entt::hashed_string{sv.data(), sv.size()};

// After: clean and intuitive
const auto key = entt::hashed_string{sv};

Implementation

constexpr basic_hashed_string(std::string_view sv) noexcept
    : basic_hashed_string{sv.data(), sv.size()} {}

Zero overhead: Single conversion to string_view, then direct delegation.

SFINAE constraint: Excludes C-string pointers to avoid ambiguity with const_wrapper used by UDL operators (_hs, _hws).

Test Coverage

  • Null-terminated string_view
  • Sized string_view (prefixes)
  • Substring in middle of buffer
  • Empty string_view
  • Single character
  • string_view with embedded nulls
  • All 184 existing tests pass

Checklist

  • Compiles without warnings (C++17)
  • All 184 core tests pass
  • Zero overhead implementation
  • Backwards compatible
  • Comprehensive test coverage

Related

/cc @skypjack

@skhaz skhaz force-pushed the feature/string_view-constructor branch from ce4b04f to f53f620 Compare April 22, 2026 14:11
Adds a convenience constructor accepting std::string_view directly,
eliminating the need for manual .data() and .size() calls.

Implementation uses SFINAE to exclude C-string pointers, avoiding
ambiguity with the const_wrapper constructor used by UDL operators.

Zero overhead: single conversion to string_view, then direct delegation
to the existing (ptr, len) constructor.

Example:
  // Before: verbose
  auto hs = entt::hashed_string{sv.data(), sv.size()};

  // After: clean
  auto hs = entt::hashed_string{sv};

Related: PR skypjack#824 (mentioned string_view as desirable)
@skhaz skhaz force-pushed the feature/string_view-constructor branch from f53f620 to 4276b40 Compare April 22, 2026 14:19
@skhaz
Copy link
Copy Markdown
Author

skhaz commented May 17, 2026

Any chance to get a review on this?

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.

1 participant