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");
}
Unlike const , constexpr can also be applied to functions and class constructors. constexpr indicates that the value, or return value, is constant and, where possible, is computed at compile time.
A constexpr function that is eligible to be evaluated at compile-time will only be evaluated at compile-time if the return value is used where a constant expression is required. Otherwise, compile-time evaluation is not guaranteed.
#define directives create macro substitution, while constexpr variables are special type of variables. They literally have nothing in common beside the fact that before constexpr (or even const ) variables were available, macros were sometimes used when currently constexpr variable can be used.
The variable declaration is not marked constexpr . The function parameter isn't constexpr . The function itself isn't marked constexpr , and it's not an immediate function. But arbitrarily we can use the value in a constexpr context.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With