Shorty: New C++ Library Offers Terser Lambda Syntax, Divides Developer Community

BigGo Editorial Team
Shorty: New C++ Library Offers Terser Lambda Syntax, Divides Developer Community

A new C++ library called Shorty has emerged, promising to simplify lambda expressions with terser syntax. The library has sparked considerable discussion among developers, with reactions ranging from enthusiasm to horror at what some see as adding another layer of complexity to an already complex language.

Shorty aims to provide a more concise alternative to C++'s standard lambda syntax, using dollar sign prefixes like $lhs, $rhs, and $it to access function arguments. This approach allows developers to write more readable code for common operations such as sorting, filtering, and transformations.

Dollar Sign Controversy

One of the most discussed aspects of Shorty is its use of the dollar sign ($) in identifiers, which several commenters pointed out isn't standard in C++. While the dollar sign has been accepted as an extension by many compilers since the early days of C, it's not part of the official language standard.

It's a common extension but it's not standard. Both C23 and C++23 only allow identifiers to start with a Unicode XID_START character, which excludes $.

The library developers were clearly aware of this limitation, as their project includes compiler options to suppress warnings about this non-standard usage. Some commenters traced this extension back to VMS systems, suggesting it might be why GCC originally included dollar sign support.

Expression Templates and Predecessors

Several developers in the comments recognized that Shorty isn't introducing an entirely new concept, but rather building upon established techniques in C++. The library uses expression templates, a metaprogramming technique that has been employed in various C++ libraries for years.

Multiple commenters compared Shorty to previous libraries with similar goals, particularly Boost.Lambda, Boost Lambda2, Boost HOF, and Boost Hana. However, there was acknowledgment that Shorty appears to be more feature-complete than some of these predecessors, offering a wider range of capabilities for argument access, function calls, and transformations.

Key Features of Shorty

  • Argument Access: $0-$9, $lhs, $rhs, $it, $a-$f, $x, $y, $z
  • Argument Queries: $argc, $args
  • Capturing: $(v), $ref(v), $value(v), $val(v), $copy(v), $fixed, $const
  • Function Calls: $<callable>(args...), $call(args...)
  • Casting: $<T>(expr), $cast(expr)
  • Tuple Creation: ($a, $b, $c), ($0, $1)
  • Assignment Operations: $a += 2, etc.

Similar Libraries

  • Boost.Lambda (obsolete)
  • Boost Lambda2
  • Boost HOF
  • Boost Hana

Mixed Reception

The community's reaction to Shorty reveals the divided perspectives on C++ metaprogramming and language extensions. Some developers expressed excitement about the library's potential, with one commenter admitting they initially disliked the syntax but were coming around to it. Others suggested they might replace the dollar sign with underscores before using it, similar to Boost::Lambda2's approach.

On the opposite end, some developers described the library as horrifying or cursed, expressing concern that it adds yet another layer of complexity to C++. One commenter specifically mentioned the potential for even more incomprehensible error messages—a notorious pain point in C++ development that once inspired a competition where the winning entry produced an error message 5.9 billion times larger than the source code.

Despite these criticisms, several technical commenters found the implementation surprisingly readable and well-structured compared to other template metaprogramming libraries, suggesting that the concept itself might have merit even if the syntax choices remain controversial.

As C++ continues to evolve with newer standards like C++23, libraries like Shorty demonstrate the ongoing tension between language purity and practical expressiveness that has characterized C++ development for decades. Whether Shorty will find widespread adoption or remain an interesting experiment remains to be seen, but it has certainly sparked thoughtful discussion about the future direction of C++ syntax.

Reference: terser (shorter) lambda == SHORTY