C15 Union Types: finally, compiler-checked “either/or” results 🚨 Tired of returning object (or building awkward base classes) just to say “this method returns A *or* B”? C15 brings a real solution: union types. ✨ What clicking gets: ✅ How `union` declares a closed set of case types (even unrelated ones) ✅ Implicit conversions so assignments stay clean ✅ Exhaustive pattern matching—the compiler forces every case to be handled (no `_` / `default` safety blanket) ✅ A clear roadmap for broader exhaustiveness in C# 🧠 Example vibe: define `Pet(Cat, Dog, Bird)` once… then `switch` with confidence knowing every case is covered. If discriminated unions were missed in C#, this is the most C#-native take yet. 🔥 https://lnkd.in/dsu5t_8M #csharp #dotnet #programming #patternmatching #compilers
Luca Congiu’s Post
More Relevant Posts
-
Why don’t more C++ developers talk about Return Value Optimization (RVO)? This optimization has existed in C++ since 1997, yet many developers still assume returning objects is expensive. Let’s take a simple case. A function creates an object and returns it: MyClass Myfun() { return MyClass obj; } Without optimization, you might imagine this happening: • create obj inside Myfun() • copy it when returning to main() • destroy the local object That sounds like extra work, right? Here’s the interesting part. Modern C++ compilers apply Return Value Optimization (RVO). Instead of: create → copy → destroy The compiler simply constructs the object directly in the caller’s memory. So the object is effectively created inside main() from the start. No extra copy. No unnecessary destruction. A subtle optimization — but one that makes returning objects cheap and idiomatic in C++. Small compiler optimizations like this are why understanding how C++ actually works under the hood matters. Did you learn about RVO early, or much later in your C++ journey? #CPP #SoftwareEngineering #Programming #SystemsProgramming #Developers
To view or add a comment, sign in
-
-
𝗟𝗼𝗰𝗸-𝗙𝗿𝗲𝗲 𝗣𝗿𝗼𝗴𝗿𝗮𝗺𝗺𝗶𝗻𝗴 𝗯𝘆 𝗛𝗲𝗿𝗯 𝗦𝘂𝘁𝘁𝗲𝗿 𝗖𝗽𝗽𝗖𝗼𝗻 𝟮𝟬𝟭𝟰 "If you think using atomics is easy, you're either jaded, one of 50 people in the world — or not being careful enough." That opening line from Herb Sutter's iconic CppCon 2014 talk Lock-Free Programming (or, Juggling Razor Blades) sets the tone perfectly. This is a two-hour deep dive that every serious C++ engineer should have in their back pocket. Here are the core ideas that stuck with me: 𝗟𝗼𝗰𝗸-𝗳𝗿𝗲𝗲 𝗶𝘀 𝗮𝗻 𝗼𝗽𝘁𝗶𝗺𝗶𝘇𝗮𝘁𝗶𝗼𝗻 𝘁𝗼𝗼𝗹, 𝗻𝗼𝘁 𝘁𝗵𝗲 𝗱𝗲𝗳𝗮𝘂𝗹𝘁 𝘀𝗲𝘁𝘁𝗶𝗻𝗴. Before using atomics, you should measure first. After writing your shiny new lock-free algorithm, measure again. Measure again. More often than not, the expected improvement simply isn't there. 𝗧𝗵𝗶𝗻𝗸 𝗶𝗻 𝘁𝗲𝗿𝗺𝘀 𝗼𝗳 𝘁𝗿𝗮𝗻𝘀𝗮𝗰𝘁𝗶𝗼𝗻𝘀. Model every member function and every locked region as a transition from one valid state to another. This mental model makes lock-free reasoning possible. A single atomic write can cause a whole graph of objects to come into existence for other threads. This is powerful, but only if you reason about it rigorously. 𝗟𝗼𝗰𝗸𝘀 𝗮𝗻𝗱 𝗮𝘁𝗼𝗺𝗶𝗰𝘀 𝗮𝗿𝗲 𝗻𝗼𝘁 𝗺𝘂𝘁𝘂𝗮𝗹𝗹𝘆 𝗲𝘅𝗰𝗹𝘂𝘀𝗶𝘃𝗲. Sutter explains producer-consumer designs in which ownership transitions between subsystems. In one phase, the transition is protected by a mutex; in the next phase, it is handed off via an atomic. The rule is simple: at any given moment in an object's lifetime, all threads must agree on how access is synchronized. This mechanism can change across phases. 𝗪𝗲𝗶𝗴𝗵𝘁-𝗳𝗿𝗲𝗲 → 𝗹𝗼𝗰𝗸-𝗳𝗿𝗲𝗲 → 𝗼𝗯𝘀𝘁𝗿𝘂𝗰𝘁𝗶𝗼𝗻-𝗳𝗿𝗲𝗲. These aren't just academic labels. They provide meaningful guarantees about system-wide throughput, starvation, and liveness. Knowing which level your algorithm achieves is important for reasoning about scalability under load. 𝗧𝗵𝗲 𝘀𝗶𝘇𝗲 𝗼𝗳 𝘁𝗵𝗲 𝗰𝗿𝗶𝘁𝗶𝗰𝗮𝗹 𝘀𝗲𝗰𝘁𝗶𝗼𝗻 𝗶𝘀 𝗮 𝘀𝗰𝗮𝗹𝗮𝗯𝗶𝗹𝗶𝘁𝘆 𝗱𝗶𝗮𝗹. The mail-slots example illustrates this beautifully. Releasing a slot before or after doing the work is a subtle yet consequential decision, and the correct answer depends on whether the bottleneck is your producer or your consumers. The code looks almost identical either way. However, the performance implications are not. 💡 If you work in systems programming, high-performance infrastructure, or any field where concurrency matters, this talk is worth your time. It won't make lock-free programming easy. But it will help you understand exactly why it isn't. 𝗥𝗲𝗳𝗲𝗿𝗲𝗻𝗰𝗲𝘀 🎥 Herb Sutter, "Lock-Free Programming (or, Juggling Razor Blades), Part I", CppCon 2014, 16 Oct 2014, https://lnkd.in/dXM3uS-v 🎥 Herb Sutter, "Lock-Free Programming (or, Juggling Razor Blades), Part II", CppCon 2014, 16 Oct 2014, https://lnkd.in/dQvQcSYx #cpp #concurrency #lockfree #cppcon #performanceengineering
CppCon 2014: Herb Sutter "Lock-Free Programming (or, Juggling Razor Blades), Part II"
https://www.youtube.com/
To view or add a comment, sign in
-
When modeling scenarios with a fixed set of outcomes — such as parsing user input or handling HTTP requests — developers often rely on enums or inheritance, which can compromise clarity and type safety. C# 15 introduces union types as a more robust alternative. These allow you to define a closed set of types with implicit conversions and pattern matching, ensuring compile-time exhaustiveness. This means the compiler will catch missing cases, reducing runtime errors. For example, a Result<int, Error> type cleanly represents success or failure, aligning with functional programming patterns and improving code safety. I believe union types offer a design shift that makes C# more expressive and safer — especially for handling domain events or API responses. What do you think — how have you handled similar scenarios before union types? #CSharp #DotNet #Programming #SoftwareDevelopment
To view or add a comment, sign in
-
Leveling up in C: Handling Strings one of the classic hurdles in C programming: String Input. While scanf() is great for single words, it fails when it hits a space. fgets(), which allows for full-sentence inputs and provides better memory safety by preventing buffer overflows. Small syntax changes, big logic wins! #CProgramming #CodingNewbie #SoftwareDevelopment #OnlineGDB #LearningToCode #include <stdio.h> #include <string.h> int main() { char name[50]; printf("Enter the first name:"); //scanf("%s",name); fgets(name,sizeof(name),stdin); printf("Hello,%s",name); return 0; } The fgets() function is the industry standard for a reason. fgets(name, sizeof(name), stdin); It captures the entire line, including spaces. Safety First: By passing sizeof(name), you tell the function exactly where to stop, preventing memory corruption. Standard Input: Using stdin ensures you are reading from the keyboard (standard input stream). practice impotant function
To view or add a comment, sign in
-
Ever wonder how the C++ compiler actually tells the difference between overloaded functions? We write functions with the exact same name and take the magic for granted. But under the hood, the linker requires a unique identifier for every single function. The secret? Name Mangling. Check out the image below! 👇 The compiler automatically alters the function's name based on the number and types of its arguments. To us, it’s just display(). To the compiler, they are two completely different entities (like i for int, d for double). The ambiguity is resolved before the code is even linked. It's a great reminder of how much heavy lifting the compiler does behind the scenes to keep our APIs clean and readable. What’s a concept you used for years before finally looking under the hood? Let's hear it below! #CPP #Cplusplus #SoftwareEngineering #TechTrivia #Programming #BackendDevelopment
To view or add a comment, sign in
-
-
Built a simple C++ Compiler from Scratch! Sharing my latest project - a simple yet functional C++ compiler built from the ground up using Flex, Bison, and ANTLR4. This project dives into the core phases of compilation and helped me strengthen my understanding of how programming languages actually work behind the scenes. Key Features: • Lexical Analysis using Flex • Syntax Parsing using Bison & ANTLR4 • Three Address Code (TAC) Generation • Basic Assembly Code Generation • Detailed logging of each compilation phase How it works: 1. Source code is tokenized into meaningful units 2. Tokens are parsed based on grammar rules 3. Intermediate code (TAC) is generated 4. Final pseudo-assembly code is produced What I learned: Building a compiler from scratch gave me hands-on experience with: • Language design fundamentals • Parsing techniques and grammar rules (Bison & ANTLR4) • Intermediate representations • Low-level code generation concepts 🛠️ Tech Stack: C++ | Flex | Bison | ANTLR4 | Makefile 📌 Example outputs include: ✔ Token logs from lexical analysis ✔ Three Address Code ✔ Assembly-like instructions This project is a small but meaningful step into the world of compilers and systems programming. 🔗 GitHub Repo: https://lnkd.in/ggkcdTyV I’d love to hear your thoughts or suggestions for improvement! #CPlusPlus #CompilerDesign #Flex #Bison #ANTLR4 #SystemsProgramming #SoftwareEngineering #LearningByBuilding #BIE #bioinformatics #BAU
To view or add a comment, sign in
-
-
Mutex might sound like the solution here, but you also essentially lose the parallelism by making the thread wait for other threads to finish work. Things LIKE atomics should be considered the defacto answer, mutexes are a last resort.
Debugging a Race Condition in C++ While working on a multithreaded module, I encountered an unexpected issue. Even though the logic looked correct, the final output was inconsistent every time. Problem: Multiple threads were updating a shared variable without synchronization. Expected: 200000 Actual: Random incorrect values After debugging, I found the root cause: Race Condition due to non-atomic operation (counter++) Key Learning: In multithreading, even a simple increment operation is not safe. It involves: Read → Modify → Write When multiple threads execute this simultaneously, data gets corrupted. Solution: Used mutex to synchronize access to shared resource. Also improved code using: lock_guard<mutex> for better safety and readability. Takeaway: Never trust shared data in multithreaded environments without proper synchronization. #cpp #multithreading #racecondition #concurrency #debugging
To view or add a comment, sign in
-
-
Solved a Gray Code problem in C++ today. The task was to generate bit patterns from 0 to 2^n - 1 such that every consecutive pattern differs by only one bit, while always starting from 0. I used the Gray code formula: gray = i ^ (i >> 1) This makes the solution clean and efficient, and guarantees that adjacent codes differ by exactly one bit. Example for n = 2: 00 -> 01 -> 11 -> 10 What I like about this problem is how a simple bit manipulation formula can solve what looks like a complex sequence-generation challenge. Concepts practiced: Bit Manipulation Binary Representation Pattern Generation C++ Problem Solving #cpp #coding #programming #datastructures #algorithms #problemsolving #bitmanipulation #leetcode #geekforgeeks
To view or add a comment, sign in
-
-
Replacing C++ with RUST might not be a good idea as C++ is stepping into a new era with a powerful framework designed to express and control concurrency and parallelism more intuitively. At its core, the model introduces three key abstractions: schedulers, senders, and receivers. These components work together seamlessly, enabling developers to compose flexible and customizable asynchronous workflows. What makes this especially exciting is how naturally it integrates with C++20 coroutines. This combination simplifies the process of writing structured concurrency, where lifetimes are cleanly nested and easier to reason about. The result? Programs that are safer by design and inherently resistant to data races. It’s not just about performance anymore—it’s about correctness and clarity at scale. Even more promising, major compilers like GCC and Clang are already ahead of the curve. They’ve implemented a significant portion of upcoming C++26 features during the standardization phase. The future of modern C++ is arriving faster than expected—and it’s built for safer, smarter parallelism. #C++2026 #OOP #Programming
To view or add a comment, sign in
-
auto is the chess pawn of C++. Ignored in the opening. Underestimated in the middle game. Then modern C++ took it to the last rank — and promoted it to queen. What once looked like an ordinary keyword has become one of the most practical and powerful features in modern C++. Today, auto helps developers write code that is: • cleaner • safer • easier to maintain • more expressive with complex types, iterators, lambdas, and templates The real value of auto is not “typing less.” It is about improving readability where the type is obvious, reducing redundancy, and letting the code focus on intent. Used well, auto is not laziness. It is modern C++ maturity. #cpp #moderncpp #softwareengineering #programming #developers #coding #cplusplus
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development