Two Features I’d Like to See in C and C++

This week, let’s discuss two features I’d really like to see in C and
C++; one trivial, one not so trivial.


The first feature I’d like to see is a rather trivial one, but one what may make things easier in low-level programs: binary notation for integers. In C/C++, we have “normal” integer notation, such as 120172, or whatever, that denotes base-10 integers as long as they do not begin by 0. A number that begins with 0 followed by digits 0 to 7, such as 06312 is in base-8, octal. While octal might have had some sense in the days where computers had odd-sized registers (12 bits?!), I don’t think it’s all that useful nowadays. Then comes hexadecimal, introduced by the prefix 0x and using “digits” 0 to 9, a to f.

I gather that binary notation would be introduced by the prefix 0b, followed by a list of bits. That’s it: 0xb01010101u. A minor itch, but one that may enhance code legibility in some cases.

* *

The second thing I’d really like, is a using clause that works pretty much like the old-time Pascal with. The with statement in Pascal introduces a scope that is relative to the variable to which with is applied. Simply:


can be translated into

with this_toaster do

A first, it doesn’t look like much of an improvement, because the second version is much longer. But now, replace the simple this_toaster by toasters[3*i+4] and we see something different. A single evaluation of the expression toasters[3*i+4] may lead to better, faster code, in addition of avoiding the unsightly repetition of a complex expression, each being the chance of inserting an error.

In C++, we already have a using statement that could be extended to this behavior (it could also be introduced to C, but maybe with would be more c-like?). C++’s using statement brings a given namespace into the current namespace, in essence yielding the union of the current namespace and of the namespace in the using statement. In C++, we can write using just about everywhere:

int something(int x, ... )
  using namespace std;
  // std:: members are accessible
  //as if from the current namespace...

What I propose is to extend using to behave “as expected” if we make follow a typename, struct, or class instance:

using some_struct {

should be equivalent to


I would expect it to work for static classes and other types, say, like this:

using std::numeric_limits<T> {
 low = min();
 hi  = max();


In this case, it’s not really all that much shorter than


but it improves legibility.

Again, we have that the argument in the using is evaluated just once, at the moment of the clause; which may lead to better code as well.

We might even introduce a this that points to the object (if not static class?) being used in the using clause in order to disambiguate scope, just as we would do in a member function, and with the same rules.

* *

I really like the idea of extending using this way. Well, there’s an element of nostalgia to it, since Pascal was my first “real” programming language, more than 30 years ago. But there’s also the old me that is getting increasingly pragmatic and I see this extended using clause as a mean of writing more legible code, reducing the chance of error, and as a way of controlling explicitly the evaluation of repetitive statements. I think it would be a great addition to the next C++ standard, and, while we’re at it, why not add it to C too?

5 Responses to Two Features I’d Like to See in C and C++

  1. Payton says:

    Did you mean 0b01 instead of 0xb01?

  2. QuantumCD says:

    Reblogged this on Contingency Coder and commented:
    I really like the ideas he has on the using statement! It would definitely improve code readability and conciseness in a lot of code bases.

  3. Funky says:

    I think the binary notation is slated for the next C++ standard, and you can already do add it via customized literals (as of C++11)

  4. Fredrik Arnerup says:

    Haven’t tried, but I think you can define binary literals yourself:

  5. Carl Tessier says:

    JavaScript has this, and for a couple of reasons its use is discouraged. Aside from name performance issue which doesn’t affect C++, it hinders readability somewhat by introducing into local scope things that have been declared elsewhere, and there’s the added risk of name conflicts, especially by introducing new names into the struct/class used in your ‘with’ or ‘using’ statement.

    Visual Basic (yes, I know…) has a relatively elegant compromise:
    With some_object
    .foo = 1234
    .bar = 5678
    End With

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: