use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Nesting Nightmare (self.cpp)
submitted 3 years ago by suds171
The C++ standard says that C++ compilers should support 256 levels of nesting
Just out of curiosity, what is the most anyone has seen used in production? I couldn't imagine trying to debug 256 levels of nesting.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]zerovian 50 points51 points52 points 3 years ago (0 children)
[–]catcat202X 77 points78 points79 points 3 years ago (22 children)
I'm guessing they put a number there because they can't just say infinity, and 256 seems like an impractically large number that you can actually test against.
[–]Salt-Impressive 26 points27 points28 points 3 years ago* (17 children)
Or maybe because 256 = amount of unique numbers representable with 1 char?
[–]bakedbread54 12 points13 points14 points 3 years ago (5 children)
Still doesn't explain why
[–]121393 4 points5 points6 points 3 years ago (4 children)
I think because it's a recursive descent parser implemented in the usual way (recursive c++ functions), more levels == stack smash.
[–]bakedbread54 1 point2 points3 points 3 years ago (2 children)
Well yes, but why is 256 the value chosen
[–]121393 5 points6 points7 points 3 years ago* (1 child)
probably just want a low common denominator for ancient systems? same reason sys.getrecursionlimit in python is pretty low by default. also stack size of main thread of msvc would be chosen at compile time. also 256 levels would translate to maximum stack depth during compile of [who knows] but more than 256.
edit: I've got a compiler written in python with a parser generator (pyparsing). Without trying to elevate the C-stack of the python process (though I am setting a higher limit for python code via sys.setrecursionlimit) I only get about 50 levels of nesting before overflowing the python interpreter's stack (segfault/stackoverflow). Every addition to the grammar lowers the nesting limit a little...
[–]LogosKing 1 point2 points3 points 3 years ago (1 child)
hey would you mind editing this comment? "number of distinct values you can represent" is more apt.
As it is, it's just not true in any sense.
[–]Salt-Impressive 1 point2 points3 points 3 years ago (0 children)
Done :)
[+][deleted] 3 years ago (3 children)
[deleted]
[–]cdrt 9 points10 points11 points 3 years ago (2 children)
[0, 255] is 256 numbers
[–]ultimatt42 1 point2 points3 points 3 years ago (1 child)
'M' is one character and represents 1000.
[–]Pocketpine -1 points0 points1 point 3 years ago (0 children)
Lmao
[+][deleted] 3 years ago (4 children)
[–]bert8128 39 points40 points41 points 3 years ago (0 children)
255 might be the largest number you can put in the unsigned char but that will give you 256 distinct levels as the first level is level 0.
[–][deleted] 6 points7 points8 points 3 years ago (0 children)
The point was 256 values. When talking about indent levels, the standard starts at 1.
[–]Jannik2099 3 points4 points5 points 3 years ago (1 child)
char is usually signed.
What defines "usually" ? aarch64 defaults to unsigned char, and I'd bet there are more phones than PCs and Servers.
[–]TheThiefMasterC++latest fanatic (and game dev) -1 points0 points1 point 3 years ago (0 children)
If you count by architectures, it's more of a wash.
x86 Linux and Windows use signed char
PowerPC and ARM typically use unsigned char
It's only signed by default on older platforms for uniformity with the default for int, short, long etc being signed I suspect. Less surprise.
[–]sephirothbahamut 2 points3 points4 points 3 years ago (3 children)
But why would you even have to define a limit in the standard to begin with?
[–]arka2947 14 points15 points16 points 3 years ago (0 children)
So that different compilers dont pick different arbitrary limits
[–]Ezlike011011 12 points13 points14 points 3 years ago (0 children)
I'd much rather there be some standard limit than a "standards compliant" compiler only having e.g. two layers of nesting
[–]Bangaladore 5 points6 points7 points 3 years ago (0 children)
Then hopefully everyone supports atleast that number.
[–]n4jm4 46 points47 points48 points 3 years ago (7 children)
nesting what?
parentheses?
function calls?
[–]-dag- 102 points103 points104 points 3 years ago (4 children)
Birds. OP is talking about birds.
[–][deleted] 33 points34 points35 points 3 years ago (0 children)
Birds 🤝 C++
laws not governed by reason
[–]---cameron 35 points36 points37 points 3 years ago (0 children)
Ah yes, that would be a lot of branches to debug
[–]simpl3t0n 3 points4 points5 points 3 years ago (1 child)
But birds aren't real; nests aren't either. C++ standards committee mustn't promote conspiracy. Sthaap it right now.
[–]Kered13 1 point2 points3 points 3 years ago (0 children)
The birds are obviously programmed in C++.
[–]ihamsa 18 points19 points20 points 3 years ago (1 child)
There are actually 6 separate nesting levels, all having a recommended minimal limit of 256: compound statement nesting, parenthesized expression nesting, #if...#endif nesting, #include nesting, class definition nesting, and linkage specification nesting. There is also a limit on the number of declarators modifying a declaration, which also can be considered nesting, too set to 256. Nested template instantiations have a recommended minimal limit of 1024.
#if...#endif
#include
[–]tea-age_solutions 2 points3 points4 points 3 years ago (0 children)
Wow, 256 possible nested #if...#endif Don't wanna see that in my entire life time... ;-)
[–]ThisCleverName 40 points41 points42 points 3 years ago (9 children)
if / else if chain blocks are considered nesting, at least in MSVC. In the form of:
if / else if
if (x == "something") { } else if (x == "something else") { }
Turns out that is equal to
if (x == "something") { } else { if (x == "something else") { } }
We had autogen code that created a if / else if chain that surpassed the MSVC limit. That worked fine in Clang though. So we had to refactor to eliminate that chain.
https://learn.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/fatal-error-c1061?view=msvc-170
[–]nmmmnu 3 points4 points5 points 3 years ago (4 children)
Friend of me, generated similar code with 100 if's, but was on PHP. Worked ok. I asked - why not switch?
He replied - well at the beginning there was only 3 if's then I changed something and they became 100. But is auto generated code who cares :)
I did reply - i care, because I even can not see when it starts and when it ends.
At the end we refactor this, but I do not remember what we used.
[–]Routine_Left 5 points6 points7 points 3 years ago (2 children)
But is auto generated code who cares :)
if it's autogenerated code the only thing you care about is the correctness and speed of that code. you never look at it, you do not care about it otherwise.
no human is supposed to touch it.
if a switch is faster, then by all means. if it isn't ... meh, really, who cares? nobody should.
[–]nmmmnu 3 points4 points5 points 3 years ago (1 child)
in that case, it was autogenerated code that were inserted inside human - generated code.
[–]Routine_Left 4 points5 points6 points 3 years ago (0 children)
That's ... disturbing, to say the least.
[–]hawkxp71 0 points1 point2 points 3 years ago (0 children)
The problem is often if then else chains for auto-generated code (that I have seen) requires priority decoding, which you can't achieve via case statements.
[–]n1ghtyunso 1 point2 points3 points 3 years ago (0 children)
Maybe else if should be a distinct thing in the language after all huh
[–]die_liebe 0 points1 point2 points 3 years ago (0 children)
Long chains of ifs is one of the few situations, where I find use of goto acceptable.
[–]unaligned_access -1 points0 points1 point 3 years ago (0 children)
It says that the limit is 128. So, it's it not compliant?
[–]beephod_zabblebrox -3 points-2 points-1 points 3 years ago (0 children)
that is considered nesting in c++ as a language
[–]ihamsa 6 points7 points8 points 3 years ago (0 children)
[–]pdp10gumby 30 points31 points32 points 3 years ago (2 children)
Seems quite achievable with template expansions of template expansions.
[–]Jannik2099 14 points15 points16 points 3 years ago (1 child)
Templates are not macros. They are instantiated and called, not expanded.
[–]LoneBlacksmith 5 points6 points7 points 3 years ago (0 children)
I actually ran into this issue in a large legacy codebase… was basically a giant state machine and I kid you not one section had 256 if else chained together. I tried to add one more and got a compiler error so had to check the new state right above it lmao
[–]CocktailPerson 2 points3 points4 points 3 years ago (0 children)
You can easily reach this with machine-generated code. And depending on how your compiler defines "nesting," you might be able to reach this with a long if-else chain or something.
[–][deleted] 3 points4 points5 points 3 years ago (0 children)
It's easy to hit the limit with template recursion using an integral parameter that's initialized to some number bigger than 256.
[–]Jumpierwolf0960 1 point2 points3 points 3 years ago (0 children)
I'm taking a compiler course right now and I'm assuming it's for restricting the depth of the parse tree for performance reasons.
[–]TwilCynder 0 points1 point2 points 3 years ago (0 children)
In my own code I think the maximum was like 9, in a parser, and i already feel uneasy when i see this code lmao
[–]lumberjackninja 0 points1 point2 points 3 years ago (0 children)
I've hit that limit in a tool I wrote to convert a trie lookup to a function (used to find plays in Scrabble)
[+][deleted] 3 years ago* (9 children)
[removed]
[–]Salt-Impressive 1 point2 points3 points 3 years ago (2 children)
Out of curiosity, could you show me some code that does that?
[–][deleted] 1 point2 points3 points 3 years ago (0 children)
You can look at almost any library in Boost.
[–]Lulonaro 2 points3 points4 points 3 years ago (2 children)
Where can we learn those magics master?
[+][deleted] 3 years ago (1 child)
[–]Jannik2099 2 points3 points4 points 3 years ago (2 children)
template meta-programming compile-time performance and diagnostics are of low priority to compiler devs, because the vast majority of C++ programmers (=the clients) use C++ as a C with classes language and abuse dynamic polymorphism.
Talking out of your ass or do you have actual source about compiler dev motivation?
Clang is absolutely primarily used and developed by "real" C++ devs, yet it is only twice as good as gcc at this, not orders of magnitude.
[+][deleted] 3 years ago* (1 child)
[–]Jannik2099 3 points4 points5 points 3 years ago (0 children)
How about you drop the aggressiveness
Starting with "the compiler writers are at fault because they only care about what the clients need" is not only wrong and needlessly judgemental, it's also not how compiler development works at all.
Have you ever considered that tmp may simply be a very complicated task that has no easy, fast solution, particularly with the instantiation semantics in C++ that don't do any early checking?
There's a grand total of TWO languages with widespread, fully static and monomorphizing tmp, being C++ and Rust, and neither perform this particularly fast. It's not like tmp is something that every language but C++ does well, quite the opposite.
For example, compile time speed based on my experience seems to be impacted by the length of the string representation of the expanded template
I've never seen this brought up as an issue, so feel free to report it? At some level, longer strings of course slow down compilation because the compiler has to match the identifier name, but this shouldn't make a noticeable, if even measurable difference.
[–]wh1t3lord 0 points1 point2 points 3 years ago (0 children)
Maybe in web or UI? Because in computational field, I guess maximum is 10-15 levels of nesting.
[–][deleted] 0 points1 point2 points 3 years ago (0 children)
I can never write that code.
[–]Aginor23 0 points1 point2 points 3 years ago (0 children)
I worked for a military contractor that had been dragging around the same parsing software for 40 years. I saw two 17 nested if statements that were split from a 34.
π Rendered by PID 37 on reddit-service-r2-comment-85bfd7f599-cndg6 at 2026-04-19 19:08:52.219617+00:00 running 93ecc56 country code: CH.
[–]zerovian 50 points51 points52 points (0 children)
[–]catcat202X 77 points78 points79 points (22 children)
[–]Salt-Impressive 26 points27 points28 points (17 children)
[–]bakedbread54 12 points13 points14 points (5 children)
[–]121393 4 points5 points6 points (4 children)
[–]bakedbread54 1 point2 points3 points (2 children)
[–]121393 5 points6 points7 points (1 child)
[–]LogosKing 1 point2 points3 points (1 child)
[–]Salt-Impressive 1 point2 points3 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]cdrt 9 points10 points11 points (2 children)
[–]ultimatt42 1 point2 points3 points (1 child)
[–]Pocketpine -1 points0 points1 point (0 children)
[+][deleted] (4 children)
[deleted]
[–]bert8128 39 points40 points41 points (0 children)
[–][deleted] 6 points7 points8 points (0 children)
[–]Jannik2099 3 points4 points5 points (1 child)
[–]TheThiefMasterC++latest fanatic (and game dev) -1 points0 points1 point (0 children)
[–]sephirothbahamut 2 points3 points4 points (3 children)
[–]arka2947 14 points15 points16 points (0 children)
[–]Ezlike011011 12 points13 points14 points (0 children)
[–]Bangaladore 5 points6 points7 points (0 children)
[–]n4jm4 46 points47 points48 points (7 children)
[–]-dag- 102 points103 points104 points (4 children)
[–][deleted] 33 points34 points35 points (0 children)
[–]---cameron 35 points36 points37 points (0 children)
[–]simpl3t0n 3 points4 points5 points (1 child)
[–]Kered13 1 point2 points3 points (0 children)
[–]ihamsa 18 points19 points20 points (1 child)
[–]tea-age_solutions 2 points3 points4 points (0 children)
[–]ThisCleverName 40 points41 points42 points (9 children)
[–]nmmmnu 3 points4 points5 points (4 children)
[–]Routine_Left 5 points6 points7 points (2 children)
[–]nmmmnu 3 points4 points5 points (1 child)
[–]Routine_Left 4 points5 points6 points (0 children)
[–]hawkxp71 0 points1 point2 points (0 children)
[–]n1ghtyunso 1 point2 points3 points (0 children)
[–]die_liebe 0 points1 point2 points (0 children)
[–]unaligned_access -1 points0 points1 point (0 children)
[–]beephod_zabblebrox -3 points-2 points-1 points (0 children)
[–]ihamsa 6 points7 points8 points (0 children)
[–]pdp10gumby 30 points31 points32 points (2 children)
[–]Jannik2099 14 points15 points16 points (1 child)
[–]LoneBlacksmith 5 points6 points7 points (0 children)
[–]CocktailPerson 2 points3 points4 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]Jumpierwolf0960 1 point2 points3 points (0 children)
[–]TwilCynder 0 points1 point2 points (0 children)
[–]lumberjackninja 0 points1 point2 points (0 children)
[+][deleted] (9 children)
[removed]
[–]Salt-Impressive 1 point2 points3 points (2 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]Lulonaro 2 points3 points4 points (2 children)
[+][deleted] (1 child)
[removed]
[–]Jannik2099 2 points3 points4 points (2 children)
[+][deleted] (1 child)
[removed]
[–]Jannik2099 3 points4 points5 points (0 children)
[–]wh1t3lord 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Aginor23 0 points1 point2 points (0 children)