146 points · by marcodiego · 4y ago · 183 comments
It's basically as if Google and Apple don't really give a damn about keeping up with C++ standards, so they always get pushed on the back burner.
That world is not today’s world. Projects that would have been written in C++ in the past are often written in a different language now. This leaves C++ being useful mostly for legacy codebases, of which adopting new standards quickly is not so important.
The reason modern C++ is still the preferred language for some types of software is that it is significantly more expressive in critical ways, which has a substantial impact on software robustness, maintainability, and performance. You could use those other languages, but it would produce a substantially worse product and/or codebase.
To put it another way, there is often no way to translate an existing elegant modern C++ codebase into those other languages without major compromises to either the codebase or performance. And for the kinds of applications where C++ is preferred, those compromises are largely unacceptable. Maybe some of those other languages will approach the practical expressiveness of modern C++ for this kind of software eventually, but not anytime in the near future.
Rust and C++ are roughly equally expressive for high-performance, high-scale software. The reasons to use one or the other for new code have little to do with whether each language can do the job; clearly, both languages can. Rather, what dominates are factors like whether your build/deployment system is set up to handle one language or another, what your engineers are experienced in, whether you care about memory safety, etc.
Source: I'm one of the maintainers of Rust at a large organization you have heard of that very much cares about high-performance, high-scale systems software.
Apple on the other hand mostly uses C++ in very restricted scopes such as DeviceKit which are very limited and can't safely use the entire language anyway. Xcode-shipped Clang and libcxx are often pretty old, too. They don't really need C++ that much above what C++03 already had, so it's clearly an afterthought for them.
Possibly an unpopular opinion, but personally I wish the C++ Standard Library was kept separate from the C++ language and have the C++ committee focus on the language features instead.
One guess is because they didn't get what they wanted within the committee.
And of course there’s also WebKit and LLVM.
I think reading it would convince you that the statement Google doesn't use modern C++ at all is false. It talks about modern features like CTAD.
The safety offered by Rust is not a thing to ignore. However modern C++ and the tooling has made enough progress in the area. It is extremely rare for me to allocate explicitly and in the couple cases where I do I always write deallocator first. It is ingrained in my nature along with some other related habits. Invalid memory access is also less likely with modern constructs. Along with memory sanitizers and practical experience with my own products I feel that Rust memory safety at this point does not offer much from my practical point of view.
I value very much time spent on compiling / building. From few experiments I made Rust is not any faster in this department. Rather the opposite.
As for C++ being a monster impossible to comprehend in its entirety. Agree and do not give a flying fuck. I am not there to explore language to its utmost depths. It is just a tool for me and nothing more. I do not get hung up on tools. As long as the tool lets me do my job with the reasonable ease (and subset of modern C++ definitely does) the rest does not matter. I do not feel inferior for not willing to spend the rest of my life trying to become Alexandrescu.
I question whether those are actually competing in this same realm, for large organizations. Language adopotion is not just about the language, but also tooling, libraries, standard, etc which organizations have built up and rely on.
For example, at Apple C++ is used more internally than is exposed in public APIS. (I can't imagine the swift compilers performance scaling up to large code bases at this point.)
Can you speculate on other reasons, besides competition for the slower rate of adoption? Is it not as helpful as previous standards? Is it more difficult to implement?
It struggles on medium sized code bases.
And Xcode becomes flaky too, on medium code bases.
Works absolutely brilliantly on the small examples that are used to market it.
I have been using it for two years, and fundamental bugs in the system have not been fixed in that time. (The code inspection tools in the debugger are close to useless)
The last time I updated it my computer spent over three hours with the installer running at 100%. What on Earth was it doing? Mining bitcoin?
A whole lot of things are clearly going very wrong behind the scenes
"For me, the reason I was enthusiastic about Go was just about the same time we were starting on Go, I read (or tried to read) the C++0x proposed standard. And that was the convincer for me." - Ken Thompson
17:45 mark here: https://www.youtube.com/watch?v=sln-gJaURzk
https://commandcenter.blogspot.com/2012/06/less-is-exponenti...
I'm pretty sure people said that about C++ when Java became popular.
Rust seems like a fine language, Zig sounds interesting, and Go and Swift I'm not familiar with. But I don't really buy your argument.
I was part of one, whose goal was to replace a CORBA infrastructure based in HP-UX, with Websphere running on Solaris/Linux.
I don't see Go as a natural C++ competitor. Go primarily competes with Java and C#.
Most of those people behind, care about the LLVM infrastructure and not so much about upstreaming clang stuff or improving ISO C++ compliance.
But yes, a lot of LLVM users don't care about C++ standard compliance at all, for example because they don't use C++.
https://nwcpp.org/April-2022.html
https://dlang.org/spec/importc.html#__import
The same technique could be used with C++.
1. add some syntax to import a module
2. create another instance of the compiler to compile that module and return the symbol table
3. have the importer, when it doesn't find a symbol in the symbol table, to search the symbol tables of the imports
[1] https://gitlab.kitware.com/cmake/cmake/-/merge_requests/7210
[2] https://gitlab.kitware.com/cmake/cmake/-/merge_requests/7210...
I was interested in C++, not javascript.
I look at Java 18, C# 11/.NET 7, Python 3.10, and the knowledge about latest features, language and ecosystem changes across all major libraries for the last 25 years is hardly less complex.
Completely disagree.
Well, you should know the libraries that you're using in C++, but I don't think that's any different in Python or Java. Granted, the mental model is more complex in C++ but that's the price you pay for the amount of control you get.
Also, isn't undefined behavior a lot easier to come across in Python since it's dynamically typed?
I find c++ syntax terse and hard to read. A good example is the well know boost library. Even back in the days when I was coding in c++ on day to day basis, I have always found boost code hard to read and reason about.
Do you mean the code of the library itself, or code using that library?
Reading the code in that page, I felt as if I looked up only to find the night sky abuzz with otherworldly ships glowing in the dark. It made me anxious. And I wanted to run.
But now I'm wondering, is this my reaction because it's been more than 10 years since I touched any c++ or because the language got really complex? I'm curious what seasoned c++ developers think about this.
It's important that they're there, authors of very generic libraries may have to deal with them. But I'd much prefer to have seen something about modules in that list...
C++ is immensely complex. The amount you need to grok is very significant.
I wonder if that is somehow a necessary feature of a performant, GC-free, optionally low level language.
Could you, for example, create something with the simplicity of Go, (most of) the speed of C/C++, and no GC? What would it look like?
I’ve never written a line of Rust so no idea there.
If you're looking for a modern, GC-less go I think it'd be your best bet unlike, say Nim, which tries to be it's own language on the syntax side too much. Zig just feels like C, but with the semantics firmed up a bit.
The C++ language designers have made a terrific language with the ergonomics of a four-fingered glove. Nim fits your criteria but doesn’t have the popularity of C++.
I wish C++ had a few more cosmetic and convenient things that I've gotten used to in Swift. 1. Properties with get/set/didSet blocks. 2. Non-nullable types, ie `T` and `T?`. There's probably no way to add those, even as a GCC-specific non-standard extension.
In Swift (and Kotlin and others) the compiler can prove that you never get a null pointer error at runtime. If you want something that can be null, the type is `T?` (short for `Optional<T>`) instead of `T`. Then there are various syntactically convenient ways of unwrapping it and dealing with the null case. Eg `foo ?? bar` means give me `foo` if it's not null, otherwise `bar`. Or `foo?.x` means: get the property x of foo, if foo is not null.
I haven't used C++ much in recent years. If there's an equivalent, I'd love to know about it.
E.g. in
std::string str;
str is never null, and always a valid object, because it's a value, not a pointer like in most GC'ed or dynamic languagesThe compiler constant folds fairly effectively anyway.
It's not exactly the same thing, but it's probably what you were thinking of.
#define QEMU_BUILD_BUG_ON(x) \
typedef char qemu_build_bug_on[(x)?-1:1] __attribute__((unused));
So you can write stuff like: QEMU_BUILD_BUG_ON(sizeof (struct foo) == 128);
(for example if the struct is used for some network protocol and so it must be 128 bytes long). #define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg)
#define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x)Starting with C11, it's possible to implement it without any non-standard extensions: https://stackoverflow.com/a/49480926
compile-time evaluation was introduced in C++11 and expanded in subsequent versions of the language standard.
Doing
const int X = 33;
// ...
void something() {
int arr[X] = ...
}
works in both C and C++, but with a catch: in C++, X is evaluated at compile time (it has internal linkage), while in C this works only after C99 because it's actually a VLA.const int X = 33;
enum { Y = X };
struct A {
int a;
int b;
A() : b(1), a(b) { }
};
. Here, the b field is used uninitialized because the order of member initializers in the member initializer list is irrelevant;
what matters is the order of declarations in the class definition.
(A related warning, -Wreorder, can be used to warn when the order of member initializers does not match the declaration order.)
I don't get it. Why b? Do they mean a? // implicitly, a = undefined;
// implicitly, b = undefined;
a = b;
b = 1;
So b is used when its value is undefined. The undefined value is assigned to a.When they say “b is used uninitialized”, they’re referring to it’s usage in that snippet. It’s uninitialized when referenced as a value for “a”.
It’s pretty easy to read that text as suggesting that b would be uninitialized in subsequent code, but that’s (not true, as you suspect, and) not what it’s talking about.
Changing the fundamental grammar is a step too far. Compilers can warn or reject bare statements in control structures if you want.
LLVM is a library that happens to have Clang in front of it, whereas GCC still isn't really able to have multiple targets in the same binary.
Could probably be done but for actually assembling toolchains you generally only need one anyway so it's not that annoying most of the time.
Not if you intend to ship on multiple platforms. Having to run builds on the native platform or install a separate cross-compilation toolchain is incredibly annoying (and difficult to get right to the point that it's common to not even try). It would be a big usability win if C toolchains could get cross-compilation to Go or Zig levels of "just works".
The most likely reason in your case is the build system of the library you want to build uses some mix of POSIX shell script and GNU Make, which is ... hard to get right on Windows. It's not C/C++ specific, through installing a cross C compiler on Windows might involve more work than rustup add target (or not, if someone did the work and packaged the toolchain as .exe installer).
clang-cl[1] does.
But then again, most new C++ features seem bananas to me, so I wouldn't read into it.
In Rust, for instance, constant functions have to evaluate the same in both compile time and run time - you can't have a different code path depending on which. If you want that, make two functions.
Trivially:
constexpr bool is_even(int x) {
if (std::is_constant_evaluated()) {
return true;
} else {
return ((x % 2) == 0);
}
}
How would you unit test this in a way that catches that only the constant evaluation is broken? constexpr bool test_is_even() {
assert(is_even(0));
assert(!is_even(1));
// … more test coverage
return true;
}
int main() {
test_is_even(); // runtime coverage
static_assert(test_is_even()); // compile-time coverage
This lets us run all test cases at both runtime and compiletime. (The static_assert performs constant evaluation, and if an assert within test_is_even would fail, it won’t be a constant expression.)This feature is added to c++ not for fun but because if you can't have a run-time branch that will do some AVX-fu, or invoke some runtime function from BLAS or LAPACK, the whole "evaluate at compile-time" thing is... Not useless but not far either