Thoughts and experiments: C-+

C-+, or cmp, Cmp, C±, is an idea that I got quite a time ago, when I was searching for an alternative to C++ for mobile devices, that could be efficient, and be able to use toolkits like Qt or GTK easily. C++ is a convenient language since it is quite low level, and have good performances, hence suited for mobile (and limited) devices. However, C++ have also a drawback: it gave a lot of power to the developer, so he / she can create wonderful things, but also quite ugly ones. C++ is the perfect language for Spiderman, since the motto “with great power comes great responsibilities” applies particularity well to it.

The lacking of “guards” also makes the language rather hard to learn. Sometimes it also has some strange syntax, that means that even experienced developers needs to be cautious. However, in modern development, we are often using only a subset of C++. We usually don’t mix pointers and references much, since references should be the preferred way to interact with objects. Exceptions are often banished, and multiple inheritance are rarely and cautiously used.

C-+ is an attempt to replace C++ for daily development, like mobile applications. It was first thought as a “compiled python”, with C++-like performances. However that target seems to be unreachable for just one hobbyist developer, so the target shifted to do a subset of C and C++ and compile it in a simpler language. It could be seen as  “the crème de la crème of C++”, with additional guards, making the code cleaner, and less error prone. However, it is still work in progress.

I will try to build a C-+ preprocessor, that will read C-+ code and output C++ code for use in classic compilers, so that they won’t be the need of doing any binding, and writing a new compiler.

Stick close to C++

C-+ tries to stick with C++ syntax, while taking inspiration from other languages, like Java. A simple hello world will look like:

int main(int argc, string *argv)
    std.cout << "Hello world !" << std.endl;
    return 0;

Basically, the definition of the main function looks a lot like in C or C++. But there are two differences:

  1. char * is replaced by string
  2. “::” is replaced by “.”

In fact, I’m still thinking about if I should keep C arrays. Usually, in C++, we either use std::vector, or arrays that some frameworks provides, like QList, so we shouldn’t bother with C-style arrays. However, array of chars are used as strings to display messages, so we should keep a basic string type, that will behave like char *.

The replacement of “::” follows the conventions that are widely used, like in Python or Java, where calling methods or classes from a module uses dots.

No references

In C-+, I also decided to get rid of the reference operator, and emphasize on pointers. However, references are still used, but only in one specific case: methods.

void setData(SomeObject *object, MyString someString)

In this method, MyString is a class, and is passed as a (const) reference to the method. The equivalent call in C++ would be:

void setData(SomeObject *object, const MyString &someString)

You can also note that I got rid of “->” and replaced it with a “.”, just like in Java and Python, once again.

No advanced keywords

C-+ tries to use the classic keywords that can be found in other languages, like if, else, switch, return etc. Advanced keywords and operators such that asm, auto, catch, extern, goto, mutable, register, throw, try, using, volatile don’t exist. Some of them are related to exceptions management, but we don’t use exceptions in C-+. Others are related to optimization, and are not often used, since I assume that the compiler is doing some great job in optimizing, and that basic applications, like mobile apps, won’t need these keywords.

I also decided introduce or change the signification of some keywords.

  1. const are not used in functions arguments since they are const references by default.
  2. immutable can only be written before a pointer declaration and means that this pointer cannot be changed. It is the equivalent of “Object * const object”
  3. final is a keyword that is used out of any block and can be used to define global constants. It is the equivalent of “static const”.
  4. “pure” virtual methods are now defined with pure virtual <type> someMethoudName() instead of virtual <type> someMethodName() = 0.

Note that unlike in Java, there is no interface keyword. An interface is simply a class with pure virtual methods.

One source file

Just like in Java, I found the ability to use one file to define and implement the code particularity convenient, so in C-+, I thought that having only one file could be nice, and not too clumsy. A class definition could look like this:

class MyObject implements QObject
    // Constructor
    MyObject(QObject *parent = 0):

    // Destructor

    // Getters and setters
    int property() const
        return m_property;
    void setProperty(int property)
        m_property = property;
    bool event(QEvent *event)
        if (event.type() == QEvent.User) {
            return false;
    // Attributes
    int m_property = 0;

We can see some more changes here:

  1. There is no more public, protected or private inheritance, and every inheritance is public. Multiple inheritance is also gone. The inheritance is done using extends (a class) and implements (an interface), just like in Java.
  2. A @macro annotation is use to call C++ macros.
  3. There is also an @override annotation, just like in Java, for marking that event is a reimplementation of QObject::event.
  4. Instead of calling QObject::event for base method, we use QObject.event, just like with namespaces.
  5. Constructors are like in C++. However, variable initialization are transferred to the declaration of attributes.

This C-+ code will generate a header and a source file. The header will only contains the definition of the class, and all signatures, while the source file will contain the implementation. It is still possible to force putting the implementation in the header, by putting the inline keyword before the method or function. This will also mark the function as inline.

Some additional guards will be set automatically. Destructors will be virtual by default, while constructors will be explicit (except copy constructor).

Imports over includes

Including classes will be done though the import statement. Just like in Java, imports will be done with dots separating the folders, like

import QtCore.*
import QtQuick.QQuickView

The following code will be translated to

#include <QtCore>
#include <QtQuick/QQuickView>

This import statement can be interesting if the C-+ compiler knows about the includes that are represented by these imports, since it could make optimizations and use forward declarations instead of includes, to speed up compilation, but this is clearly not for the initial release.

However, this leads to a problem if you want to import classes declared in the project tree, especially in parent folders, as “..” will clash with the “.” separator.

A lot to think about and no compiler yet

This project is still in a very early stage of work, as I’m learning about flex and yacc, and that I did not thought about all the possible corner cases. Some topics, like the definition of templated classes or methods, are not investigated yet, while others, like the import system, needs to be polished more.

You are free to join the discussion, especially if you have some good ideas, or corner-cases to showcase. You can do it either here, in the comments session, or on Twitter or IRC, or any place where you can find me. 🙂


I forgot to mention that some inconsistencies in C / C++ are removed as well. For example, you will need to use braces in if / else statement, even if the statement is one line long.


2 thoughts on “Thoughts and experiments: C-+

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s