Skip to content

Instantly share code, notes, and snippets.

@27Cobalter
Created February 17, 2026 15:06
Show Gist options
  • Select an option

  • Save 27Cobalter/8c71ab67c0d0f16e8f6427b1d00ac060 to your computer and use it in GitHub Desktop.

Select an option

Save 27Cobalter/8c71ab67c0d0f16e8f6427b1d00ac060 to your computer and use it in GitHub Desktop.
#include <cstdint>
#include <print>
#include <string_view>
class A {
public:
static constexpr const char* Key1 = "Key1";
static inline const char* Key2 = "Key2";
static inline const char* const Key3 = "Key3";
};
consteval bool IsValidKey(std::string_view key)
{
return (key == "Key1") || (key == "Key2") || (key == "Key3");
}
template <typename T>
void tmp()
{
if constexpr (IsValidKey(A::Key1) == false) {
static_assert(false);
}
// if constexpr (IsValidKey(A::Key2) == false) {
// static_assert(false);
// }
if constexpr (IsValidKey(A::Key3) == false) { // cl compilable, but clang & gcc eror
static_assert(false);
}
}
auto main() -> int32_t
{
std::println("Key1: {}, Key2: {}, Key3: {}", A::Key1, A::Key2, A::Key3);
// A::Key1 = "NewKey1"; // Error: cannot modify a constexpr variable
A::Key2 = "NewKey2"; // Allowed: Key2 is a static inline variabl
// A::Key3 = "NewKey3"; // Error: cannot modify a const variable
constexpr auto key1_valid = IsValidKey(A::Key1);
// constexpr auto key2_valid = IsValidKey(A::Key2);
constexpr auto key3_valid = IsValidKey(A::Key3); // cl compilable, but lint & gcc eror
std::println("{} is a valid key: {}", "Key1", IsValidKey(A::Key1));
// std::println("{} is a valid key: {}", "Key1", IsValidKey(A::Key2));
std::println("{} is a valid key: {}", "Key1", IsValidKey(A::Key3)); // cl compilable, but lint & gcc eror
std::println("Key1: {}, Key2: {}, Key3: {}", A::Key1, A::Key2, A::Key3);
tmp<int32_t>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment