Код: Выделить всё
consteval auto funcNameLength(const std::source_location &Loc = std::source_location::current()) {
const char *Name = Loc.function_name();
std::size_t Length = 0;
while (Name[Length] != '\0')
++Length;
return Length;
}
int main() {
constexpr auto L = funcNameLength(); // L == 4
}
< /code>
или это (с strlen strlen): < /p>
consteval auto
funcNameLength(std::size_t Length = constexpr_strlen(
std::source_location::current().function_name())) {
return Length;
}
int main() { constexpr auto L = funcNameLength(); }
< /code>
Однако для меня этого недостаточно. Мне нужно использовать длину в качестве постоянного выражения, чтобы что-то сделать, прежде чем вернуться к абоненту, например, < /p>
consteval auto
doSomething(std::size_t Length = constexpr_strlen(
std::source_location::current().function_name())) {
int A[Length]; // Error: Length is not a constant expression.
}
int main() {
doSomething();
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... ioncurrent