How can I obtain the length of a string at compile time?
Date: 2022-12-27Last modified: 2023-03-07
Table of contents
Objective
To obtain, at compile time, the length of a compile-time string constant.
☢
Warning
The problem is that the strlen
function is not constexpr
.
Solution
constexpr len1 = std::string::traits_type::length("Geraldo");
// or
constexpr len2 = std::char_traits<char>::length("Ribeiro");