Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
deleted 205 characters in body
Source Link
Aykhan Hagverdili
  • 30.5k
  • 6
  • 53
  • 108

You can also use a std::array<char, N>. The sample below requires C++20:

#include <array> // std::array #include <cstddef> // std::size_t template <auto constexpr_string> void needs_constexpr_string() { // ... use the string ... } #define STR(s) ( template <auto \N>  [] () consteval {  \ auto constexpr szstr(char =const sizeof(s&cstr); [N]) \{   std::array<char, sz>N> arr; \   for (std::size_t i = 0; i < sz;N; ++i) \   arr[i] = s[i];  \cstr[i];   return arr; \  }() \ )  int main() { needs_constexpr_string<STRneeds_constexpr_string<str("Hello World")>(); } 

You can also use a std::array<char, N>. The sample below requires C++20:

#include <array> // std::array #include <cstddef> // std::size_t template <auto constexpr_string> void needs_constexpr_string() { // ... use the string ... } #define STR(s) (  \  [] () consteval {  \ auto constexpr sz = sizeof(s);  \   std::array<char, sz> arr; \   for (std::size_t i = 0; i < sz; ++i) \   arr[i] = s[i];  \   return arr; \  }() \ )  int main() { needs_constexpr_string<STR("Hello World")>(); } 

You can also use a std::array<char, N>. The sample below requires C++20:

#include <array> // std::array #include <cstddef> // std::size_t template <auto constexpr_string> void needs_constexpr_string() { // ... use the string ... } template <auto N> consteval auto str(char const (&cstr)[N]) { std::array<char, N> arr; for (std::size_t i = 0; i < N; ++i) arr[i] = cstr[i]; return arr; } int main() { needs_constexpr_string<str("Hello World")>(); } 
Source Link
Aykhan Hagverdili
  • 30.5k
  • 6
  • 53
  • 108

You can also use a std::array<char, N>. The sample below requires C++20:

#include <array> // std::array #include <cstddef> // std::size_t template <auto constexpr_string> void needs_constexpr_string() { // ... use the string ... } #define STR(s) ( \ [] () consteval { \ auto constexpr sz = sizeof(s); \ std::array<char, sz> arr; \ for (std::size_t i = 0; i < sz; ++i) \ arr[i] = s[i]; \ return arr; \ }() \ ) int main() { needs_constexpr_string<STR("Hello World")>(); }