C++, modules, etc.
While writing this, my daughter asked me what a Knork is (h/t NYT Mini, Sep 3rd, 2020). It felt like it fit. Image credit: Jason Eppink, Wiktionary

C++, modules, etc.

Everyone seems to be running to embrace Rust. I spent some time last summer learning the basics through Rustlings, and I enjoyed the ideas behind it - but it seems like Rust requires significant up-front design as part of managing memory ownership. I don't mind that: up front design is one of the key differences between "development" and "engineering". I worry that the structure of the design is so fundamental when using Rust, though, that it will be difficult to refactor and change the structure of the program as needed, leading to more rewrites. I don't have experience with large Rust codebases yet, so I don't know how real that concern is. It is pretty common for actual users to need something different than predicted users, and to need to do significant refactoring.

I continue to learn and play with C++. Most people I've spoken with disagree with this sentiment, but I still believe that C++ will evolve "safe enough" practices sooner than people will feasibly port away from it. By "Safe Enough" I mean that the rate of memory safety issues will go down to the point where it's no longer the most pressing problem in the code. Stroustrup and Sutter both have written posts about "safety profiles", and a lot of effort has gone into the C++ Core Guidelines. With modern C++, it is much easier to avoid pointer arithmetic, disable pass-by-value for classes, and avoid other gotchas. It's still harder than it needs to be, but I'm betting that the competitive pressure from Rust will light a fire under making this happen in the next few years. To add evolutionary pressure to C++ we need Rust to be successful now and ongoing.

In order to better understand modern C++, I like to do experiments and use new idioms in my open source code. Today's hacking time was spent testing C++ modules. Clang has supported a form of modules for a while, but as of June 12th, libc++ has marked the experimental module support as complete. So I wanted to try it! With C++23, hello world can look like this:

import std;

int
main()
{
    std::print("Hello, world\n!");
    return 0;
}        

(I just realized I could've done std::println. Oops!)

I love the idea of not having to think about STL includes, and how using C++ modules can help people "Include What You Use". So I followed the instructions on the libc++ website and it works! If you want to try it, my repository is set up to work well with GitHub Codespaces and will build an Ubuntu 24.04 container. You will need to install Homebrew, and install cmake from HEAD. Then install clang 19 from apt.llvm.org, making sure that you install libc++-19-dev and clang-tools-19. I used VSCode in my Codespace, so it should work with the usual CMake workflows: Configure, Scan for kits, and then build it.

I'm looking forward to the CMake 3.30 and the LLVM-19 releases!

(obDisclosure: I work for Google, but I am not speaking for them in this post)

Peter Ludemann

Retired (software)

8 个月

I've seen far too much C++ (and C) code where not enough thought was given to memory management/ownership and fixing leaks / use-after-free becomes a nightmare. I'm tempted to say that C++ code shouldn't allow bare pointers but everything should be std::unique_ptr or std::shared_ptr (and - for experts - std::weak_ptr). How is that different from Rust?

回复
Jeremy Manson

Itinerant Software Engineer, Former Googler, Parent. Not in that order.

8 个月

I've spent a while in the Rust ecosystem. It requires more upfront time, but some of that is just because some of the patterns you use in C++ don't work, and you have to develop an intuition for that. Quite a few people will start to write as they would in C++, get far enough to realize they can't do it that way, and then just give up and use reference counting instead of refactoring their code. :/

Jeff Bailey

Engineering Manager at Google

8 个月

This is my first article using LinkedIn's platform. I'm curious how it will go. I dislike Medium because I can't tell before I click on an article if it will cost money to read or not. And I don't really want to run my own blogging service.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了