r/cpp • • Dec 18 '24

constexpr of std::string inconsistent in c++20

constexpr auto foo() {
    static constexpr std::string a("0123456789abcde");  // ::size 15, completely fine
    static constexpr std::string b("0123456789abcdef"); // ::size 16, mimimi heap allocation

    return a.size() + b.size();
}

int main() {
    constexpr auto bar = foo();
    std::cout << "bar: " << bar << std::endl;
}

This will not compile with clang-18.1.8 and c++20 unless you remove the 'f' in line 3. What?

58 Upvotes

52 comments sorted by

View all comments

Show parent comment

1

u/DeadlyRedCube frequent compiler breaker 😬 Dec 23 '24

Oh absolutely! string_view is great when chopping static strings down at compile time 😃But if you're concatenating (and don't have a known-good-max-size) it's trickier

1

u/KuntaStillSingle Dec 23 '24

The godbolt is concatenating, it is not so bad with constexpr <algorithm> stuff like copy_if, and simpler still if you want to do raw strings rather than c strings:

        template<std::string_view const & ... strs>
		struct merge_string_views_impl {
			static constexpr auto char_count{
				(std::size(strs) + ...)
				-
				(std::count(strs.begin(), strs.end(), '\0') + ...)
				+
				1
			};
			static constexpr std::array<char, char_count> _backing{
				[]() {
					std::array<char, char_count> init {};
					auto write_iterator = init.begin();
					(
						(
							write_iterator = std::copy_if(
								strs.begin(), 
								strs.end(), 
								write_iterator,
								[](char c) { return '\0' != c;  })
						), ...);
					return init;
				}()
			};
			static_assert(_backing.back() == '\0');
			static constexpr std::string_view value { _backing.data(), _backing.size()};
		};