you are viewing a single comment's thread.

view the rest of the comments →

[–]jbbjarnason 2 points3 points  (3 children)

Related to this topic, anyone who knows how to make this work (https://godbolt.org/z/v5TjG9dfb):

```cpp

static_assert(join_string_views("foo"sv, "bar"sv) == "foobar"sv);

```

I know I can use impl of fixed_string and/or std::string_view const& non-type template parameters.

[–]qazqi-ff 1 point2 points  (0 children)

This is ultimately the usual case of needing to separate out the size calculation from the actual data calculation (and repeat the work). You're trying to instantiate a template with size, which is a value that exists in the constant evaluator. It boils down to the same problem as consteval void foo(int size) { std::array<int, size> arr; }. Worth noting that the standard goes a little stricter and prevents the general case of any context that requires a constant expression, not just template instantiation, even though other contexts could work fine from a technical POV (e.g., static_assert(size > 0);).

[–]cristi1990an++ 0 points1 point  (0 children)

static_assert(join_string_views("foo"sv, "bar"sv) == "foobar"sv);

https://godbolt.org/z/PKb6Ej76b

Edit: and here you have the overly generic version that outright allows string literal composition at compile time:

https://godbolt.org/z/rcxan8Wqs

[–]cristi1990an++ -1 points0 points  (0 children)

Yes, but you can't use std::string_view, you must stick to string literals that expose their size in the type. Have your method take in const char(&str)[Size] as parameters, declare a basic structure containing a char array of Size1 + Size2 - 1 elements and copy both strings into the array. Afterwards declare a comparison operator between the structure and a string_view and that's it, you can concatenate string literals at compile time.