Apparently, the constexpr std::string has not been added to libstdc++ of GCC yet (as of GCC v11.2).
This code:
#include <iostream>
#include <string>
int main()
{
constexpr std::string str { "Where is the constexpr std::string support?"};
std::cout << str << '\n';
}
does not compile:
time_measure.cpp:37:31: error: the type 'const string' {aka 'const std::__cxx11::basic_string<char>'} of 'constexpr' variable 'str' is not literal
37 | constexpr std::string str { "Where is the constexpr std::string support?"};
| ^~~
In file included from c:\mingw64\include\c++\11.2.0\string:55,
from c:\mingw64\include\c++\11.2.0\bits\locale_classes.h:40,
from c:\mingw64\include\c++\11.2.0\bits\ios_base.h:41,
from c:\mingw64\include\c++\11.2.0\ios:42,
from c:\mingw64\include\c++\11.2.0\ostream:38,
from c:\mingw64\include\c++\11.2.0\iostream:39,
from time_measure.cpp:2:
c:\mingw64\include\c++\11.2.0\bits\basic_string.h:85:11: note: 'std::__cxx11::basic_string<char>' is not literal because:
85 | class basic_string
| ^~~~~~~~~~~~
c:\mingw64\include\c++\11.2.0\bits\basic_string.h:85:11: note: 'std::__cxx11::basic_string<char>' does not have 'constexpr' destructor
How will such strings work under the hood when a string contains more than 16 char
s (because GCC's SSO buffer size is 16)? What would be a brief explanation? Will a trivial constructor create the string object on the stack and never use dynamic allocations?
This code:
std::cout << "is_trivially_constructible: "
<< std::boolalpha << std::is_trivially_constructible<const std::string>::value << '\n';
prints this:
is_trivially_constructible: false
Now by using constexpr
here (obviously does not compile with GCC v11.2):
std::cout << "is_trivially_constructible: "
<< std::boolalpha << std::is_trivially_constructible<constexpr std::string>::value << '\n';
will the result be true
like below?
is_trivially_constructible: true
My goal
My goal was to do something like:
constexpr std::size_t a { 4 };
constexpr std::size_t b { 5 };
constexpr std::string msg { std::format( "{0} + {1} == {2}", a, b, a + b ) };
std::cout << msg << '\n';
Neither std::format
nor constexpr std::string
compile on GCC v11.2.