Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do C++ STL function calls need to be so verbose?

Why can't calls to STL functions be more brief? I was looking at the following code snippet on cppreference.com:

#include <string>
#include <cctype>
#include <algorithm>
#include <iostream>

int main()
{
    std::string s("hello");
    std::transform(s.begin(), s.end(), s.begin(),
                   [](unsigned char c) { return std::toupper(c); });
    std::cout << s;
}

It seems to me that it should be possible to make this call more brief. The first obvious thing would be to do away with the lambda:

std::string s("hello");
std::transform(s.begin(), s.end(), s.begin(), std::toupper);
std::cout << s;

But this doesn't work. Since you usually want to convert a whole container it should be possible to just use that as a parameter:

std::string s("hello");
std::transform(s, s.begin(), std::toupper);
std::cout << s;

You could also omit the output iterator to return the result by value:

std::string s("hello");
std::cout << std::transform(s, std::toupper);

At this point the temporary variable is unnecessary:

std::cout << std::transform("hello"s, std::toupper);

With added readability:

using namespace std;
cout << transform("hello"s, toupper);

Wouldn't this be more readable and just nicer? Why weren't STL functions designed to allow writing brief code like this? Will it be possible to shorten these calls in a future version of the C++ standard?

like image 849
Szabolcs Avatar asked Sep 29 '16 11:09

Szabolcs


1 Answers

Unfortunately, std::toupper has overloads, so the lambda is the workaround.

Then, with range-v3, you can simplify it to:

auto to_upper = [](unsigned char c) { return std::toupper(c); }; // To handle overloads
std::cout << std::string("Hello world" | ranges::view::transform(to_upper))
          << std::endl;

Demo

like image 129
Jarod42 Avatar answered Sep 19 '22 00:09

Jarod42