r/cpp • u/kyan100 • Mar 01 '26
Should C++ Give More Priority to Syntax Quality?
I’ve been thinking about this for a while and I’m curious what others here think.
The C++ committee does an incredible job focusing on zero overhead abstractions and performance. But I sometimes feel that syntax quality and aesthetic consistency don’t get the same level of attention.
Recently I’ve noticed more features being proposed or added with syntax that feels… awkward or visually noisy. A few examples:
co_await- The reflection operator
^^ - The proposed
e.try?try operator
And many other similar things. Individually none of these are catastrophic. But collectively, I worry about the long-term impact. C++ is already a dense and complex language. If new features keep introducing unusual tokens or visually jarring constructs there is a risks of becoming harder to reason about at a glance.
Should syntax design be given equal weight alongside performance?
I’d love to hear perspectives from people who follow WG21 discussions more closely. Is this something that’s actively debated? Or is syntax mostly a secondary concern compared to semantics and performance?
Curious to hear what others think.
6
u/HommeMusical Mar 01 '26
I basically agree with a lot of your points; your examples aren't as good.
This
?has been very successful in other languages, IIRC JS and Java, to deal with the similar but definitely not the same issue of nullability. But I can't find this proposal for C++, and the one I remember didn't use a?Compact, pretty clear - not too bad.
The rest of the comments here are just full of much more horrible constructs than the ones you are complaining about. :-D
215
u/blipman17 Mar 01 '26
This ship has so sailed already
85
u/FlyingRhenquest Mar 01 '26
shared_ptr, unique_ptr, dynamic_pointer_cast, guys? Abbreviate or not abbreviate but do it consistently! lol. Admittedly fairly low on the abomination scale compared to, say, trying to splice into some nested templates or something but it gets me fairly regularly.
28
u/argothiel Mar 01 '26
Yes, while designing new features, naming and syntax is a huge part of the discussion. I would say, in some cases it's even the majority of the discussion, and sometimes the reason why the feature is delayed. The proposals and discussions are open, you can check the reasoning yourself.
So yes, everybody is doing their best and putting a lot of thought into that matter and somehow we end up with terrible results anyway. We can only hope that they are the least evil of choices, and that the alternative would be worse.
60
u/TotaIIyHuman Mar 01 '26
some font replace
>=with≥you can get a font to replace
^^with🐱28
u/ts826848 Mar 01 '26
The reflection proposal has multiple sections/references devoted to explaining their choice of syntax and the evolution thereof.
I can't find solid references so I'm not 100% sure about this, but I want to say the
co_prefixes were added to reduce the chance of breaking existing code.Not familiar with the try proposal and a quick glance at the papers github didn't seem to turn up anything relevant. Do you have a link?
13
u/__christo4us Mar 01 '26
There were a lot of discussions regarding the syntax for reflection. The form of the reflection operator
^^had many different candidates. One of them was a singular^but Objective-C already uses^for block declarations and adopting^as a reflection operator would lead to conflicts so^^was adopted instead.Previously, a more verbose form
reflexpr(...)was considered but it would introduce a new keyword that is not distinct enough from function call syntax and it would be too verbose if, for instance, you wanted to initialize a collection of reflection values from initializer list.People liked the aesthetics of
^and^^because reflection operation is sometimes thought of as a raising operation. In the end, what matters is that^^is concise, distinct and easy to type and search for in source code.25
u/BarryRevzin Mar 01 '26
I'm a little surprised to see a post talking about syntax that is "awkward or visually noisy" and then use as examples:
co_await, which is an 8-character keyword, whose comparison would be to a 5-character keyword, that's used in contexts where the extra 3 characters... doesn't really matter all that much? I get that this is the butt of jokes, but that extraco_really doesn't have significant syntactic cost.^^e, which is a two-character operator, where both of those characters are themselves mostly-whitespace. Compared to people wantingreflexpr(e), which is a 10-character operator, which is very significantly more visually noisy.To me "awkward or visually noisy" would be our lambda syntax, where a lambda to check if the input is negative takes 28 characters (unless you really hate your readers and skip whitespace):
[](auto e){ return e < 0; }, whereas in plenty of other languages it's less than half that, likee -> e < 0or|e| e < 0. We don't even have to go full Haskell(<0). Swift has a seemingly-verbose-compared-to-Java/JS/Scala/C#/Rust's{ e in e < 0 }, but even that is half of ours and they provide an even terser form than that. Lambdas are kind of peak visual noise.When I think of bad syntax in C++, I think of the following.
What does
int()mean? Of course, it means the type "function taking no parameters and returningint." Were you thinking something else? That's a big problem. The ambiguity we have between variable/function declarations continues to cause real issues (typingco_is not a real issue). In general, the declarator syntax that we inherited from C is just awful.For example, what is this:
This actually isn't even that complicated a declaration, conceptually, but is already illegible. And everybody knows this is illegible, which is why nobody would ever dream of writing it in code like that (you would either make a type alias, use trailing-return syntax, or both). I would bet that a significant percentage of experienced C and C++ programmers would take a decent chunk of time just to figure out which of
fandgup there is actually the name of... whatever this is declaring.This syntax approach is also related to why our array declarations are just wrong (
int x[10];instead ofint[10] x;), and also related to why abbreviating lambdas is a challenge, coming up with syntax for reflecting expressions, and so on.Other choices for bad C++ syntax in my mind are:
<and>for template arguments, due to the ambiguity withoperator<. Other solutions in this space are D'stuple!(int, char), Zig's just using parens liketuple(int, char), or Rust requiring::s in contexts where it could be ambiguous, so liketuple::<int, char>.T(a)andT{a}be usually the same thing but sometimes also very different and also the first one is actually a cast.decltype(e)anddecltype((e))similarly be a very subtle differentiationtemplate <std::convertible_to<int> T>. But then also not recognizing the first parameter is special in any other context, sostd::convertible_tois still actually a binary concept, and now that we have concept template parameters, you cannot actually "pass"std::convertible_to<int>as a concept argument, because it isn't one.The concept syntax also ties into one of my pet peeves about C++ syntax, which itself is the result of having syntax that is precisely "awkward or visually noisy." And that is: how do you declare the
mapalgorithm foroptional<T>? The way that you very frequently see this in C++ talks is:Which has the benefit of being pretty decently legible, but has the downside of being wrong. Also unnecessarily inefficient, but more importantly just wrong. I cannot call this with a lambda, I can only call this with literally a
function<Sig>of some sort. Instead you have to write this:Which isn't exactly terrible per se, but also this is one of the simplest cases and it's already pretty awkward and visually noisy. Now try to write
and_thenwith the correct constraint.Compare this to Rust's syntax, which is significantly cleaner:
or
53
u/Jovibor_ Mar 01 '26
co_awaitand^^look very childish in front oftrivially_relocatable_if_eligible9
u/victotronics Mar 01 '26
Structured binding. Nice. Range based for loops and range syntax. Concise. I think the language is improving.
10
u/James20k P2005R0 Mar 01 '26
This was litigated extensively internally, and alternatives were rejected. There wasn't a technical reason, but its what the authors of reflection preferred
I personally suspect the reflection syntax may have been better with a different set of choices, but its just a case of time now. Perhaps in the future we'll deprecate the old syntax and swap it, but I doubt it
17
u/TomDuhamel Mar 01 '26
Do you have suggestions of unused symbols to use for the syntax of new features? No? Well that's why.
Syntax is definitely part of the debate there. There's just so few options left to use. And the introduction of new keywords is being avoided at all cost because of the high risk of breaking existing code.
15
u/lucidbadger Mar 01 '26
You are asking about a language with
std::moveandstd::copyin the standard library. What syntax quality are you talking about?7
u/triconsonantal Mar 01 '26
I'm more annoyed by how overloaded the syntax tends to be.
decltype(x)is ugly, but whatever. It'sdecltype((x))anddecltype(auto)that's pushing it.11
u/wrd83 Mar 01 '26
https://github.com/hsutter/cppfront
People have thought about it?
3
u/RoyBellingan Mar 01 '26
What is the problem with co_await ?
15
u/riztazz Mar 01 '26
I like C++ syntax :( It's the most readable language to me
8
u/MarcoGreek Mar 01 '26
If you look up the discussion about co_await and ^ you will see there was an extensive debate. await is simply too widely used and C++ cares deeply about backward compatibility. The same for . I would prefer a different approach.
^ is me a lucky accident because it will not be that widely used and is much easier to spot. I missed the not “!" already that often that I prefer more visible operators.
And think about uniform and initializer lists. That only happened because they wanted to use a really short syntax for initializer lists.
5
u/TheoreticalDumbass :illuminati: Mar 01 '26
^^ is actually good tho
2
u/Otaivi Mar 01 '26
It’s okay I think. I like how verbose and descriptive it is, many languages try to shorten the letter count and not conflict with other existing usage or common programming lingo but they sometimes end up with something worse as the meaning becomes indirect. C++ is quite straightforward to understand and is mostly consistent. But I would prefer that they would have standardised certain abbreviations like with the smart pointer all being abbreviated to ptr but certain pointer operations like static_pointer_cast did not do that. I think my only gripe is with std::move (all of them) where the mental image it gives is not what it actually does-ish. I also think that there isn’t an exact word or phrase that succinctly describes what it does elegantly, so we’re stuck with ‘move’
2
u/celestabesta Mar 01 '26
We make fun of java for public static void main string args, but if you want to write a method in c++ that covers all bases you can get way worse easily.
nodiscard maybeunused static constexpr auto foo(...) const noexcept(noexcept(a.~T())) -> rettype
2
u/chibuku_chauya Mar 01 '26
There’s a great deal of “expert friendly” syntax in the language but I find that once I learn it it’s fine and it’s no longer a problem. One can adapt to almost anything. It simply requires repeated exposure.
1
u/Raknarg Mar 01 '26
the language already has so much syntax. I don't think there's any alternatives that wouldnt come with problems. I noticed you didn't have any alternatives in this post
1
u/EmotionalSplit8395 Mar 02 '26
C++ prioritizes ABI stability and backward compatibility this often forcing ergonomic compromises to avoid breaking decades of existing code. 🤕
1
u/Liam_Mercier Mar 02 '26
I think the syntax is generally ok considering how large the language is, you need to remember some identifier for the feature anyways.
1
u/FragmentedHeap Mar 05 '26
C++23 and modules have been in the standard for a while. Msvc is the only compiler with production ready support.
I gave up on adoption a long time ago. What's it matter whats in the specs if they take 5+ years to be implemented in every compiler? Modules are amazing and I refuse to come back till they're everywhere.
-1
u/dexter2011412 Mar 01 '26 edited Mar 01 '26
I'm this close to
#define await co_awaitIt's just better to use literally any other language at this point
Edit: damn people taking this seriously not realizing I'm joking
1
u/fdwr fdwr@github 🔍 Mar 01 '26
Another aspect to consider with any new syntax is tooling complexity. I wish C++ decided decades ago to use a distinct template parameter syntax rather ambiguously overloading "<" and ">" to either be less-than/greater-than or template parameters, requiring dictionary parsing to just decide which is which (or ">>" to either be a right shift operator vs closing parentheses). Last week I wrote a simple code text wrapping tool (for really long lines) which works great for something like MLIR, and mostly works for C++, except that ambiguity :(. At least those above examples don't appear to greatly complicate tool parsing.
1
u/Inevitable_Gas_2490 Mar 02 '26
Yes please. Been working with C# for a while now and every time I gotte revisit some c++ projects, I miss the staggering amount of syntax sugar that C# brings that speeds up productivity and makes code actually readable
0
u/_Noreturn Mar 01 '26
I really wish C++ provided unsafe accessors to everything in the stl for max performance things like accessing internal variant storage as void* and such
0
u/JeffMcClintock Mar 02 '26
ugghh.. co_await incenses me.
anyone who's IDE can't search and replace any legacy use of "await" with something else is not to be taken seriously. And who's to say someone's codebase doesn't already contain a variable called "co_await" anyhow??? The "co_" prefix fixes nothing.
9
u/[deleted] Mar 01 '26
[removed] — view removed comment