//******************************************************** // The following code example is taken from the book // C++20 - The Complete Guide // by Nicolai M. Josuttis (www.josuttis.com) // http://www.cppstd20.com // // The code is licensed under a // Creative Commons Attribution 4.0 International License // http://creativecommons.org/licenses/by/4.0/ //******************************************************** #include #include #include #include #include "asstring.hpp" // function template to export a compile-time string to runtime: template consteval auto toRuntimeString(std::string s) { // ensure the size of the exported array is large enough: assert(s.size() <= MaxSize); // create a compile-time array and copy all characters into it: std::array arr{}; // ensure all elems are initialized for (int i = 0; i < s.size(); ++i) { arr[i] = s[i]; } // return the compile-time array and the string size: return std::pair{arr, s.size()}; } // function to import an exported compile-time string at runtime: std::string fromComptimeString(const auto& dataAndSize) { // init string with exported array of chars and size: return std::string{dataAndSize.first.data(), dataAndSize.second}; } // test the functions: consteval auto comptimeMaxStr() { std::string s = "max int is " + asString(std::numeric_limits::max()) + " (" + asString(std::numeric_limits::digits + 1) + " bits)"; return toRuntimeString<100>(s); } int main() { std::string s = fromComptimeString(comptimeMaxStr()); std::cout << s << '\n'; }