简体   繁体   中英

Why does my function only work with lvalues?

I have a function that returns a lowercase string:

constexpr auto string_to_lower_case(const std::string& string) {
    return string
        | std::views::transform(std::tolower)
        | std::views::transform([](const auto& ascii) { return static_cast<char>(ascii); });
}

and I expect that function return the same resultat when i will pass "SOME" or const std::string some("SOME") , but it's not. When I try to print out a result of string_to_lower_case("SOME") , I retrieve an empty console (the output of string_to_lower_case(some) is correct)

const std::string some("SOME");
for (const auto& ch : string_to_lower_case(some))
    std::cout << ch;

Some issues:

  • The temporary std::string that is created when you call the function with a char[] goes out of scope when the function returns and is then destroyed. The view you return can't be used to iterate over the string after that.
  • You take the address of std::tolower which isn't allowed since it's not on the list of Designated addressable functions .
  • You don't convert the char used with std::tolower to unsigned char first. If char has a negative value, it'll cause undefined behavior .
  • Your second transform ation seems redundant.

An alternative is to return an actual std::string instead of a view :

constexpr std::string string_to_lower_case(const std::string& string) {
    auto view = string
              | std::views::transform([](char ch){
                    return static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
                });
    return {view.begin(), view.end()};
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM