What alternative to C++?
What language is the best candidate to replace C++ after 35 years of existence and millions of crashes due to its unsafe syntax?
A successor to C++ must produce very fast applications and code working on all platforms. But we can also speak of succession to a language that uses exactly the same syntax then C, adds classes and provides more advanced functions. So, a modern C++ even if it is not intended to replace it everywhere.
Vala (2002)
Available at Gnome, look like a demonstration of GObject, written by the same team. Vala replace C++ on the Gnome platform.
Features:
- Compile to C.
- Based on GObject which adds a class system in C.
- Uses GType types which allows to interface multiple languages.
- Memory automatically handled by reference counting.
- Interface with other language by vapi files.
- Compatible with GTK.
Simple example:
int main () {
print ("Hello World\n");
return 0;
}
Class:
class Hello : Object {
void bye () {
stdout.printf ("Hello World\n");
}
}
var example = new Hello ();
example.bye();
Many programs have been written in Vala, but even if it can be used outside the Gnome environment, it is essentially in combination with GTK. The project is still active in 2022 with a new website, vala.dev.
D (2001)
Designed as an alternative to C++ with simplifications such as dynamic arrays.
- Compatible with C (but not C++).
- Simple inheritance and mixins .
- Concurrent actors.
- Memory managed by a garbage collector. It can be disabled, but then the compatibility with libraries is lost.
Simple example:
import std.stdio;
void main() {
writeln("Hello World!);
}
Class:
class Hello {
char[] content;
this(char[] str) { // constructeur
content = str;
}
void display() {
writeln(content);
}
}
Hello hello = new Hello("Hello World!");
C# (2000)
Since the Roslyn platform, available on GitHub, C# can be compiled into binary code rather than bytecode, and it can access the system resources, so even if it was originally an alternative to Java, it becomes a possible replacement to C++, for applications where the garbage collector is not a drawback.
Features:
- Compiles to bytecode or binary.
- Works on all operating systems including mobiles.
- Can be combined with other .NET languages. Compatible with C++ in managed mode on .NET.
- Parallel programming.
- Memory managed by a garbage collector.
Simple example:
void Main() {
Console.WriteLine("Hello, World!");
}
Class:
public Class Point : Shape {
public int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
public void move(int w, int h) {
this.x += x;
this.y += h;
}
}
Point p = new Point(0,0);
p.move(100,50);
Dart (2013)
Designed as an alternative to JavaScript with the syntax of C, this is not a system language, but it can replace C++ for scripting on the server or the desktop with a speed close to that of Asm.js.
- Works on the Fletch Virtual Machine based on V8.
- Classes and inheritance.
- Static or dynamic variables.
- Concurrency with isolates.
- Operator overloading.
- Might also be compiled into JavaScript.
Simple example:
void main() {
print('Hello World!');
}
Class:
class Point {
num x, y;
Point(this.x, this.y); // constructeur
void move(num xo, num yo) {
x += xo;
y += yo;
}
}
var p = new Point(0, 0);
p.move(100, 50);
Other languages
There are other languages derived from C or to have a vocation to replace C++ as system language.
- Go. It has replaced C++ at least at Google who designed it for this purpose. Its domain is that of Web services, which is only a partial target of C++. This is certainly an area where C++ has no future.
- Java. Its syntax is a modernization of that of C++, but its principles are very different. One file per class, running on a virtual machine. It is especially used for Web applications which puts it in competition with Go.
- Rust. Mozilla language working on the LLVM virtual machine. Designed for portable applications, it has a different syntax but also, for safety purposes, adds major constraints to the programmer. C and C++ owe their success to the fact that the programmeur can do what he wants in these languages. Despite the publicity given to it by Mozilla followers, the language ranks very low in popularity statistics.
- Nim. Its syntax is similar to that of Python and its audience is currently small. We discuss it because some see it as a successor to C++ as a system language. It is compiled to C like Vala and uses a garbage collector, the latter should be non-blocking for use of Nim to games and the system.
- Asm.js. This subset of JavaScript, is about as fast as native code and as portable as C and it can be used for 3D games with WebGL. If the browser is made an alternative to the operating system, it can actually replace C++ for immersive applications such as games (for example, the Unreal Engine has a C++ and a Asm.js version).
Finally, what successor to C or C++? If we rely on the classification of languages by popularity, none yet! C remains the most widely used language and C++ stands in third place, after Java and before C#.
The emerging trend is that a set of languages, each in a specific field: game development, web application, scripting, etc ... may very well together replace C++, a universal language that can not be replaced everywhere by a single language.
This gives its only real advantage to the syntax derived from C: the programmer can pass more easily from one language to the other to make different types of applications.