Skip to content

Instantly share code, notes, and snippets.

@ahmedalkabir
Last active October 27, 2022 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmedalkabir/cf1696fa5659b36d6fadc5ecf6e5cd85 to your computer and use it in GitHub Desktop.
Save ahmedalkabir/cf1696fa5659b36d6fadc5ecf6e5cd85 to your computer and use it in GitHub Desktop.
constexpr size_t operator""_str(const char* str, size_t size) noexcept
{
size_t digest = 0;
while (size-- > 0) {
char val = *str;
digest ^= val * val ^ val; // worst hash algorithm
++str;
}
return digest;
}
struct sstring : public std::string
{
using std::string::string;
operator std::size_t() noexcept(noexcept(std::string::c_str()))
{
return operator""_str(std::string::c_str(), std::string::size());
}
};
int main(int argc, int** argv) {
sstring str = "lambda";
switch (str) {
case "ahmed"_str:
cout << "you're ahmed\n";
break;
case "lambda"_str:
cout << "You're wierd\n";
break;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment