Interview: Bjarne Stroustrup on 21st Century C++, AI Risks, and Why C++ Remains Indispensable
C++ inventor Bjarne Stroustrup recently shared his insights on modern C++ practices, the challenges of replacing the language, the risks associated with AI in coding, and the benefits of having multiple compilers. This interview provides valuable perspectives for both seasoned C++ developers and those new to the language.
Modern C++: A Coherent Whole
Stroustrup emphasizes that modern C++ is not just about the latest features but about using the language as a coherent whole, combining features from its earliest days to the present. He advocates for writing code that is more direct, efficient, expressive, and safe.
Key Features of Modern C++
- Expressiveness and Directness: Stroustrup highlights the importance of expressing intentions directly in the code. For example, using range-based for loops (
for x in y
) to iterate over containers simplifies the code and reduces the chance of errors. - Generic Programming: Leveraging generic programming allows the compiler to deduce types, ensuring type safety and reducing the need for explicit type declarations.
- Resource Management: Proper resource management is critical. Stroustrup advises using Resource Acquisition Is Initialization (RAII) to ensure resources are properly managed and leaks are avoided. Every resource should be owned by a handle within a scope, ensuring automatic cleanup.
Practices to Avoid in Modern C++
Stroustrup also outlines practices that modern C++ developers should avoid to write safer and more efficient code:
- Raw Pointers as Resource Handles: Avoid using raw pointers to manage resources. This can lead to memory leaks and other resource-related issues.
- Passing Elements by Raw Pointer: Never pass a set of elements using a single raw pointer, as it lacks information about the number of elements and prevents proper range checking. Use containers like
std::vector
instead. - Excessive Use of Casts: Minimize the use of casts, as they can introduce type errors. Generic programming helps reduce the need for explicit casts.
- Returning Data via Free Store: Avoid allocating memory on the free store and passing a pointer out of a function. Instead, use move semantics to return vectors or other containers, which is often zero-cost.
Leveraging Modules, Templates, and Concepts
Stroustrup also highlights the importance of modules, templates, and concepts in modern C++:
- Modules: Use modules with the
import
statement instead of the older#include
. Modules offer better encapsulation, avoid transitive inclusion, and reduce compilation times. - Templates and Concepts: Templates enable generic programming, and concepts (introduced in C++20) provide constraints on template parameters, making code easier to understand and maintain.
Enforcing Modern C++ Style
Enforcing a modern C++ style across large codebases is a challenge. Stroustrup is working on guidelines enforced by profiles, which specify a set of guarantees that the compiler can enforce. However, he expresses frustration that the C++ standards committee has not yet guaranteed the inclusion of this feature in C++26.
In the meantime, developers can use tools like Clang-Tidy, which checks for compliance with the C++ core guidelines, a joint project with Red Hat, Microsoft, and others.
The Impact of AI on C++ Coding
Stroustrup expresses concerns about the impact of AI on C++ coding. While AI tools can be helpful, he fears that they may guide developers towards outdated practices and reduce their ability to detect problems independently.
He notes, "I'm not saying it won't help, but it does have a tendency of guiding people towards things which everybody used to do. It's not a good way forward. Furthermore, I fear that people lose the ability to detect problems because they are so used to having it done for them."
The Advent of New Languages and C++'s Enduring Strength
The emergence of new languages, some based on C++ like Google's Carbon project, raises questions about the future of C++. Stroustrup acknowledges that it's easier to design a language that's better than C++ for a specific domain. However, C++'s strength lies in its ability to work in diverse domains.
He cautions that if new languages succeed, they must be able to interact with C++, Python, and other languages. Otherwise, the industry risks ending up with multiple insufficient languages, each struggling to interoperate.
The Pace of C++ Evolution
The pace of C++ evolution is a topic of debate. Stroustrup notes that half the people complain it's too slow, and the other half complain it's too fast. While he would like to see the standards process move faster, he acknowledges that the committee's size and diverse concerns slow things down.
The Benefits of Multiple C++ Compilers
The various C++ compilers differ in their implementation of the official specification. While this can create friction for developers, Stroustrup argues that it's a good thing. Each major compiler has a large user base, and he doesn't like monocultures.
He explains, "If you look at history, monocultures, it just takes on bug or one kind of poison and you're dead." The existence of multiple compilers fosters competition, innovation, and prevents complete stagnation.
Stroustrup acknowledges that the implementations in major C++ compilers are not identical but are getting closer. He believes that the competition between compilers is beneficial for the language's evolution.
Key Takeaways from the Interview
Here are the key takeaways from the interview with Bjarne Stroustrup:
- Embrace Modern C++: Use the language as a coherent whole, combining features from its earliest days to the present.
- Avoid Outdated Practices: Steer clear of raw pointers as resource handles, excessive casting, and returning data via the free store.
- Leverage Modern Features: Utilize modules, templates, and concepts to write more efficient and maintainable code.
- Be Mindful of AI: Use AI tools with caution, and don't rely on them to the point where you lose your ability to detect problems independently.
- Appreciate C++'s Strengths: Recognize C++'s ability to work in diverse domains and its enduring relevance in the programming landscape.
- Understand the Compiler Ecosystem: Appreciate the benefits of having multiple C++ compilers, as they foster competition and innovation.
The Enduring Legacy of C++
Bjarne Stroustrup's insights provide a valuable perspective on the current state and future direction of C++. By embracing modern practices, avoiding outdated techniques, and understanding the evolving landscape of programming languages, developers can continue to leverage the power and versatility of C++ for years to come.
C++ remains a cornerstone of software development, and Stroustrup's guidance ensures that developers can harness its full potential in the 21st century.