fenbf / cppstories-discussions

4 stars 1 forks source link

2021/constexpr-vecstr-cpp20/ #54

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

constexpr vector and string in C++20 and One Big Limitation - C++ Stories

constexpr started small in C++11 but then, with each Standard revision, improved considerably. In C++20, we can say that there’s a culmination point as you can even use std::vector and std::string in constant expressions! Let’s look at use cases, required features to make it work, and finally, one significant limitation that we might want to solve in the future.

https://www.cppstories.com/2021/constexpr-vecstr-cpp20/

nickhuang99 commented 3 years ago

Both clang and gcc(https://godbolt.org/z/7ncaqKhz8) do not support this feature. Is this new feature pipelined in c++23?

fenbf commented 3 years ago

yep, @nickhuang99 - as I wrote at the bottom of the article: As of August 2021, this feature works only in one major compiler - MSVC, starting from Visual Studio 2019 16.10

grizzlysmit commented 3 years ago

interesting ah well something to look forward to.

kobi-ca commented 3 years ago

splitSV? where is the def?

fenbf commented 3 years ago

@kobi-ca skipped in text, but it's defined in the playable version at CE

fenbf commented 3 years ago

see more comments at reddit/cpp: https://www.reddit.com/r/cpp/comments/pehseo/constexpr_vector_and_string_in_c20_and_one_big/

TheMR-777 commented 2 years ago

My constexpr solution

Well, below is my constexpr solution for the last problem:

auto find_longest(const std::string_view s) noexcept -> std::string_view
{
    auto splitted = s | std::views::split(" "sv);
    auto max_elem = std::ranges::max(splitted, {}, [](const auto x) { return std::string_view{ x }.size(); });
    return max_elem;
}

constexpr auto max = find_longest("Hi, it's TheMR from Pakistan");

Try it: https://godbolt.org/z/55dabn1Tf

jxu commented 1 month ago

Apparently, constexpr dynamically allocated memory IS possible with lambdas.

Create a lambda that makes a vector, and use another lambda to create an array

https://stackoverflow.com/a/72415591