<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/c-26-trivial-infinite-loops-are-no-longer-undefined-behaviour-pytlyicjx" -->

---
title: C++26: Trivial infinite loops are no longer undefined...
description: C++26 fixes a long-standing quirk where a trivial infinite loop like while (true); with no side effects was undefined behaviour, allowing compilers such as...
canonical: https://daily.dev/posts/c-26-trivial-infinite-loops-are-no-longer-undefined-behaviour-pytlyicjx
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: C++26: Trivial infinite loops are no longer undefined behaviour | daily.dev
og:description: C++26 fixes a long-standing quirk where a trivial infinite loop like while (true); with no side effects was undefined behaviour, allowing compilers such as...
og:url: https://daily.dev/posts/c-26-trivial-infinite-loops-are-no-longer-undefined-behaviour-pytlyicjx
og:image: https://api.daily.dev/og/posts/PYtlYICJX.png
og:image:alt: C++26: Trivial infinite loops are no longer undefined behaviour
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# C++26: Trivial infinite loops are no longer undefined behaviour

**[Sandor Dargo](https://daily.dev/sources/sandordargo)** · 5 min read · 2 upvotes · 0 comments

## Summary

C++26 fixes a long-standing quirk where a trivial infinite loop like while (true); with no side effects was undefined behaviour, allowing compilers such as Clang to optimize it away entirely and fall through to unrelated code. Via proposal P2809R3, C++26 introduces the concept of a 'trivial infinite loop' (empty body plus a constant-true controlling expression) which is now well-defined: the body is replaced with std::this_thread::yield(), giving it forward-progress semantics. This matters for embedded and kernel code, where while(true); is a common halt-on-error pattern that previously risked falling through into corrupt execution. C already handled this correctly since C11; C++26 closes the gap with a narrower rule than C's, and on freestanding implementations the yield-replacement is implementation-defined to avoid breaking intentional halt loops.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.sandordargo.com/blog/2026/09/16/cpp26-trivial-infinite-loops>

## Questions this post answers

### Is while (true); undefined behavior in C++26?

No, C++26 makes trivial infinite loops well-defined via proposal P2809R3. A trivial infinite loop is one with an empty body (just ; or {}) and a constant expression controlling condition that evaluates to true. When both conditions hold, the compiler replaces the loop body with a call to std::this_thread::yield(), giving it forward-progress semantics instead of undefined behavior.

_Track C++26 changes like this on daily.dev before they surprise you in embedded or systems code._

### Why was while(true); undefined behavior in C++ before C++26?

Because of the forward-progress guarantee introduced in C++11, which lets the implementation assume a thread will eventually terminate, do I/O, touch a volatile glvalue, or perform synchronization. An empty while(true); loop does none of these, so under pre-C++26 rules the optimizer could assume execution never actually gets stuck there and remove the loop entirely, causing execution to fall through to whatever code follows, as demonstrated by Clang printing an unrelated 'Hello world!' after optimizing away the loop.

_Developers debugging compiler-driven surprises like this rely on daily.dev to stay current on C++ semantics._

### Was while(1); always undefined behavior in C, unlike C++?

No, while(1); has been well-defined in C since C11. C's forward-progress rules include an extra clause stating that loops whose controlling expression is a constant expression may not be assumed to terminate, a rule C++11 never adopted, creating an unnecessary divergence between the two languages that persisted until C++26 introduced its own narrower fix with trivial infinite loops.

_Comparing C and C++ semantics quirks like this is easier when tracking language spec changes on daily.dev._

## Similar posts on daily.dev

- [C\+\+26: Reducing undefined behaviour](https://daily.dev/posts/c-26-reducing-undefined-behaviour-tbbsne1hb) · Sandor Dargo · 0 upvotes · 0 comments
- [One of the best C\+\+29 features is already available?](https://daily.dev/posts/one-of-the-best-c-29-features-is-already-available--yusxlus0o) · Meeting C\+\+ · 5 upvotes · 0 comments
- [C\+\+26 is done\! — Trip report: March 2026 ISO C\+\+ standards meeting \(London Croydon, UK\)](https://daily.dev/posts/c-26-is-done-trip-report-march-2026-iso-c-standards-meeting-london-croydon-uk--qgbxvzmu3) · Sutter's Mill · 18 upvotes · 1 comments

---

Tags: [#embedded](https://daily.dev/tags/embedded), [#c++](https://daily.dev/tags/c++), [#compiler-optimization](https://daily.dev/tags/compiler-optimization)

[View this post on daily.dev](https://daily.dev/posts/c-26-trivial-infinite-loops-are-no-longer-undefined-behaviour-pytlyicjx)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"C++26: Trivial infinite loops are no longer undefined behaviour","url":"https://daily.dev/posts/c-26-trivial-infinite-loops-are-no-longer-undefined-behaviour-pytlyicjx","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/c-26-trivial-infinite-loops-are-no-longer-undefined-behaviour-pytlyicjx"},"datePublished":"2026-09-16T12:39:33.232Z","dateModified":"2026-09-16T12:39:57.956Z","description":"C++26 fixes a long-standing quirk where a trivial infinite loop like while (true); with no side effects was undefined behaviour, allowing compilers such as...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/bc56fe31dfb8fc6b112ce220016c1fe6?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/bc56fe31dfb8fc6b112ce220016c1fe6?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Sandor Dargo","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Sandor Dargo","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/48d17993e5864d66951193405fa786bb","url":"https://daily.dev/sources/sandordargo"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/c-26-trivial-infinite-loops-are-no-longer-undefined-behaviour-pytlyicjx","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"embedded,c++,compiler-optimization","timeRequired":"PT5M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Sandor Dargo","item":"https://daily.dev/sources/sandordargo"},{"@type":"ListItem","position":3,"name":"C++26: Trivial infinite loops are no longer undefined behaviour"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/c-26-trivial-infinite-loops-are-no-longer-undefined-behaviour-pytlyicjx#faq","mainEntity":[{"@type":"Question","name":"Is while (true); undefined behavior in C++26?","acceptedAnswer":{"@type":"Answer","text":"No, C++26 makes trivial infinite loops well-defined via proposal P2809R3. A trivial infinite loop is one with an empty body (just ; or {}) and a constant expression controlling condition that evaluates to true. When both conditions hold, the compiler replaces the loop body with a call to std::this_thread::yield(), giving it forward-progress semantics instead of undefined behavior. Track C++26 changes like this on daily.dev before they surprise you in embedded or systems code."}},{"@type":"Question","name":"Why was while(true); undefined behavior in C++ before C++26?","acceptedAnswer":{"@type":"Answer","text":"Because of the forward-progress guarantee introduced in C++11, which lets the implementation assume a thread will eventually terminate, do I/O, touch a volatile glvalue, or perform synchronization. An empty while(true); loop does none of these, so under pre-C++26 rules the optimizer could assume execution never actually gets stuck there and remove the loop entirely, causing execution to fall through to whatever code follows, as demonstrated by Clang printing an unrelated 'Hello world!' after optimizing away the loop. Developers debugging compiler-driven surprises like this rely on daily.dev to stay current on C++ semantics."}},{"@type":"Question","name":"Was while(1); always undefined behavior in C, unlike C++?","acceptedAnswer":{"@type":"Answer","text":"No, while(1); has been well-defined in C since C11. C's forward-progress rules include an extra clause stating that loops whose controlling expression is a constant expression may not be assumed to terminate, a rule C++11 never adopted, creating an unnecessary divergence between the two languages that persisted until C++26 introduced its own narrower fix with trivial infinite loops. Comparing C and C++ semantics quirks like this is easier when tracking language spec changes on daily.dev."}}]}
```

