case1:
#include <string>
inline constexpr std::string test(std::string s) noexcept
{
return s + "xxx";
}
int main()
{
auto s = test("abc");
}
c++20 with gcc 12.1 is built okay, c++17 with gcc 12.1 or c++20/17 with gcc 11.1 was failed,
constexpr std::string, Is this a new feature, or what does this mean?
case2:
#include <string>
int main()
{
constexpr std::string test{"xxxxxx"};
}
And in this case both failed, what is the difference between these two cases.
The following might not compile either, for you:
There's a slight semantical difference between a
constexprobject and aconstexprfunction.A
constexprobject must be a compile-time constant.A
constexprfunction might be a compile-time constant if everything in the function is a constant expression. Subsequently, the result of the function is a constant expression if its parameters are, and obviously not if they're not.What appears to be happening is that your compiler hasn't yet implemented the
constexprversion of thestd::stringconstructor in C++20, but has implemented theconstexpr+operator.Hence the function gets compiled without a constant expression as its parameter, but since its result is not assigned to a
constexprobject this is not immediately apparent.But assigning the function's result to a
constexprreveals the ugly truth.