Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8131073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:58:04+00:00 2026-06-06T08:58:04+00:00

I want to implement of sort of Symbol the same way ruby does. For

  • 0

I want to implement of sort of Symbol the same way ruby does.

For this, I created a user defined literal which returned a std::hash of the std::basic_string<T> corresponding.

The code was great, but as I read somewhere the hash function may not be consistent over several executions of the same program. Moreover, I wanted to make this computation at compile-time, which was 1) not supported by std::hash and 2) would break code if std::hash return value changes.

So I wrote up the following implementation, based on java.lang.String.hashCode implementation.

typedef size_t symbol;

template<typename CharT>
constexpr size_t constant_hash(const CharT* p, size_t h = 0) noexcept
{
    return (*p == 0) ? h : constant_hash(p + 1, h * 31 + static_cast<size_t>(*p));
}

constexpr symbol operator "" _sym (const char* p, size_t n) noexcept
{
    return constant_hash(p);
}

My question is: are there any problem with this implementation?

I’m only able to test it on GCC 4.7.1, and I’d like to know if it is standard-compliant and
should also work on other compiler.

I’m asking this because a previous implementation was working on GCC but caused segfault if the binary was compiled with clang++ (problem of undefined behavior with increment operators I think).

Thanks in advance

Edit

Work with clang++ (thanks KennyTM)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T08:58:06+00:00Added an answer on June 6, 2026 at 8:58 am

    There’s no UB, it works fine, as long as the string has '\0' termination. Note that constexpr evaluation cannot invoke UB; arithmetic or pointer operations that cause UB at runtime are required to produce a compile error in the context of a constant-expression.

    Note that the static_cast is unnecessary; the char operand will be promoted to size_t.

    Also, on first glance the hash function doesn’t look very good because h * 31 is just ( h << 5 ) - h. You might pick a larger number with 1’s randomly spread out across entire the binary value. But on the other hand, they might be trying to be clever since the low 5 bits of ASCII has the most entropy and this eliminates the possibility of collisions between short strings of different length.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to implement a sort of feedback form/survey form in asp.net which is
I want to implement a delegate solution for Bubble sort. I have this code:
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
I want to implement file uploading with behaviour which is commonly seen in various
I want to implement a Facebook updates(user action and notifications) on my web application
I want to implement some sort of lookup table in C++ that will act
I want to implement search bar in my application which will filter list view
I want to implement a sort of a singleton object ( About in the
I have the table which list some details from database.I want to sort the
I want to implement a sort of backup feature for my backup app, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.