Why doesn't this compile:
Could there be a problem with a string
as a return type?
constexpr std::string fnc()
{
return std::string("Yaba");
}
Why doesn't this compile:
Could there be a problem with a string
as a return type?
constexpr std::string fnc()
{
return std::string("Yaba");
}
The constructor of std::string
that takes a pointer to char
is not constexpr
. In constexpr
functions you can only use functions that are constexpr
.
constexpr
is that it has side-effects (namely allocation) that can’t be carried out at compile time. –
Obie constexpr
2. have a member-initializer part involving only potential constant-expressions and 3. have an empty body. It seems to me that std::string
has to violate #2. –
Obie constexpr
constructor for std::string
that takes arbitrary length const char*
and be safe. While it's true that string literals are constant expressions, there are plenty of things of type const char*
that are not constant expressions. The c_str
member of std::string
comes to mind. Overloading on constexpr
is not available to distinguish between the two. –
Pitarys © 2022 - 2024 — McMap. All rights reserved.