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 8460403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:35:51+00:00 2026-06-10T13:35:51+00:00

Any computation is possible at compile-time with C++ template metafunctions. Therefore, I was considering,

  • 0

Any computation is possible at compile-time with C++ template metafunctions. Therefore, I was considering, if the following were possible:

void my_function(char const* string_ptr)
{
  switch (hash_function(string_ptr))
  {
    case hash_metafunction<"yoohooo">::value:
      ...
      break;

    case hash_metafunction<"woooooo">::value:
      ...
      break;

    ...
  }
}

Can you give leads as to where to find code (library) for both the hash function and template metafunction. If none such library exists, can you give hints on how I might roll the template metafunction myself? I am particularly worried about the char const* parameter to the template metafunction. Maybe some preprocessor magic is possible?

  • 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-10T13:35:53+00:00Added an answer on June 10, 2026 at 1:35 pm

    How about a constexpr function? Of course implementing that hash could be a pain. You’ll have something like this:

    // maybe another return type
    constexpr uint64_t hash_metafunction(const char* input) {
        // replace some_value with the hash implementation
        return some_value;
    }
    
    void my_function(char const* string_ptr)
    {
      switch (hash_function(string_ptr))
      {
        case hash_metafunction("yoohooo"):
          ...
          break;
    
        case hash_metafunction("woooooo"):
          ...
          break;
    
        ...
      }
    }
    

    The hash_metafunction function would be executed on compile-time.

    Edit: This is a naive implementation, which basically converts the input string to a uint64_t:

    constexpr uint64_t do_the_hash(const char* input, uint64_t value_so_far) {
        return *input ? do_the_hash(input + 1, (value_so_far << 8) | *input) : value_so_far;
    }
    
    constexpr uint64_t hash_metafunction(const char* input) {
        return do_the_hash(input, 0);
    }
    

    Live demo here.

    Edit: I’ve implemented a compile time MD5, you can find the source code here. In order to use it, do the following:

    #include <iostream>
    #include "md5.h"
    
    int main() {
        constexpr auto value = ConstexprHashes::md5("constexpr rulz");
    
        std::cout << std::hex;
        for(auto v : value) {
            if(((size_t)v & 0xff) < 0x10)
                std::cout << '0';
            std::cout << ((size_t)v & 0xff);
        }
        std::cout << std::endl;
    }
    

    This prints out the hash: “b8b4e2be16d2b11a5902b80f9c0fe6d6”.

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

Sidebar

Related Questions

Is it possible to enter an infinite loop at compile time? My program seems
How can I measure compilation/built time in Visual Studio 2010? Have any advices how
Any tips on making the following option elements draggable? <select id=expenseSelect multiple=multiple style=height:200px> <option
Is there any way to compile BlackBerry Java code for different sections using eclipse
Are there any asm instructions that can speed up computation of min/max of vector
Is there any way to dynamically and seamlessly migrate a program / computation from
Possible Duplicate: How do Trigonometric functions work? What actually goes into the computation of
I'm searching a way to include __LINE__ as a compile-time constant in outputted messages.
Possible Duplicate: Have you used any of the C++ interpreters (not compilers)? Hi, I
After writing an answer to this question which displays the solution at compile time

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.