Notes on "C++ and Beyond"

Last year I was lucky enough to attend to a set of talks by Scott Meyers, Herb Sutter, and Andrei Alexandrescu titled “C++ and beyond”. It was a true pleasure to listen, all three are very good talkers. I would like to summarize my personal opinions and thoughts on what I have heard. There were a few things mentioned that are seen as strong points in C++ (actually, C++14). Those included

  1. Anonymous functions (lambdas).
  2. Automatic type inference (although they never used that phrase – they call it type deduction).
  3. “Batteries included”: include many base libraries with standard compilers.
  4. Polymorphism
  5. Concepts (still not in C++14 … sigh)
  6. Methods for parallelism / transactional memory (also, still not in C++14)
  7. Move semantics
  8. Return value optimization
  9. Portability of C++ code

While anonymous functions are available in C++, it doesn't change the fact that the syntax is ugly and comparably unreadable. Look at this code:

std::vector<int> a = {1,2,3,4,5};
auto pr = [](auto a) {
for_each(a.begin(), a.end(),
     [](auto a) { std::cout << a << " "; }); };
for_each(a.begin(), a.end(), [](auto& a) { a = a * 2; });
pr(a);

which does more or less the same as this

let a = map (*2) [1,2,3,4,5] in print a

Now, I am not completely sure whether there is a shorter or more readable way to write the above C++ code, but I have a feeling that there is not.

Another thing I remember from the talks is the syntax of the so-called *lambda init capture*. Use your favourite search engine to find out about it, I personally think it hurts my brain.

Concerning type deduction using, for example, the auto keyword, Meyers stated that there are at least five different ways in which the compiler determines a type, depending on where in the code the type is occurring. That does not sound like something I want to use a lot, even though I am a big fan of automatic type inference in general. Also, the error messages from the current compilers in cases where a type mismatch happens can be very unhelpful (lots of template error messages).

I wanted to know just how smart auto is, so I tried it out while listening. What would you think happens here:

auto a = 42;
auto b = 1 << 36;
long long int c = (long long int)1 << 36;
auto d = (long long int)1 << 36;
long long int e = 1 << 36;

Go ahead and try. I used g++ 4.9.2 and together with a few lines for printing, it gave me:

Length of a: 4
Length of b: 4
Length of c: 8
Length of d: 8
a: 42
b: 0
c: 68719476736
d: 68719476736
e: 0

It works for d, but there you told the compiler what the type needs to be. So you don't need auto, really. And even for e it does not work, because the compiler assumes the 1 on the right hand side to be an int.

Check out Haskell:

let a = 42
-- GHCi says: a :: Num a => a
let b = 1 ‘shiftL‘ 36
-- GHCi says: b :: (Bits a, Num a) => a
print b
-- results in 68719476736.

Seems smarter, really.

Concerning the batteries included concept, I like that very much. It's just that it kind of doesn't really work with C++ up until now, and other languages have been doing just that for a long time, such as Python, Haskell, and Java.

C++'s Concepts are still not there, while Haskell has it's nice typeclasses.

Parallelism, on the other hand, seems to be a big issue also in the future development of C++. This is good, but still, there is no way for the compiler to assure that functions that may be running in parallel are not affecting each other. A pure language can do that, and Alexandrescu said something along the lines of “add purity to the language before trying to add STM”, with which I can only agree. Again, Haskell has purity and also Control.Concurrent.STM.

Another interesting remark that Meyers made was “It's ok to learn new syntax if the code gets more readable”. I completely agree with that, and that is also an argument for learning a different language.

Please don't get this wrong, I liked to be there and I enjoyed the talks. There was a lot of very interesting information about optimizing code and some stories from the field, which I enjoyed hearing.

Nevertheless, I am much less a fan of C++ than I used to be years ago, and I am promoting the use of more modern languages in practice wherever that is possible.

2015/01/22 22:06 · Christian · 0 Comments

Version Control Systems

All version control systems (VCS) suck in one way or the other. It's just that some suck less than others, and of the VCS that I have used over many years so far, the one that is the absolute winner of crappiness is called MKS Integrity. It is a miracle to me how people get used to this monstrosity of non-usability and slow operation, paired with unsafe operations that make your local changes to the code you are writing vanish with a click of a mouse button. If anyone does ever get used to it, that is.

How a system as bad as MKS was conceived, or more likely how it has congealed, I can only speculate. Not only does it appear to be a threat to your code base, it also makes your developers work slower. Who in the world could want something like that? Sigh.

2013/10/10 22:45 · Christian · 0 Comments

Best Of Jelly Bean

I recently got Android 4.1 on my phone, and the very best thing about it so far is this: it finally has a Colemak on-screen keyboard, which is slightly less idiotic than QUERTY. Yay.

2013/05/01 15:07 · Christian · 0 Comments

TypeMatrix Special Keys In XMonad

I got a TypeMatrix Keyboard recently. It has a few buttons for starting up some applications directly, which did not work out of the box in my xmonad setting.

A little playing with xev lets us add a few keys to ~/.xmonad/xmonad.hs:

import XMonad                                                                                             
import XMonad.Config.Xfce                                                                                 
import XMonad.Util.EZConfig (additionalKeys)                                                              
 
main = xmonad $ xfceConfig                                                                                
         { modMask = mod4Mask,                                                                            
           terminal = "xfce4-terminal"}                                                                   
         `additionalKeys` [ ((mod4Mask, xK_F5), spawn "virtualbox --startvm Vista"),                      
                            ((mod4Mask, xK_F2), spawn "emacs"),                                           
                            ((mod4Mask, xK_F3), spawn "firefox"),                                         
                            ((mod4Mask, xK_F4), spawn "thunar"),                                          
                            -- Typematrix special keys                                                    
                            -- fn-sleep:                                                                  
                            ((0, 0x1008ff2f), spawn "dbus-send --system --print-reply --dest=org.freedesk\
top.UPower /org/freedesktop/UPower org.freedesktop.UPower.Suspend"),                                      
                            -- Web browser                                                                
                            ((0, 0x1008ff18), spawn "firefox"),                                           
                            -- EMail                                                                      
                            ((0, 0x1008ff19), spawn "evolution"),                                         
                            -- Calculator                                                                 
                            ((0, 0x1008ff1d), spawn "xfce4-terminal -e \"ghci\"")                                                                                                     
                          ]

This works very well for me, let me know if you make use of this and whether you needed to change anything!

2012/05/07 22:16 · Christian · 0 Comments

Jalla is on Hackage Now

jalla is now also available on hackage and can be downloaded with cabal. It does not build on hackage because lapacke is not available there, so see the above link for the documentation (which is also not built on hackage).

2012/04/01 20:20 · Christian · 0 Comments

Older entries >>

 
blog.txt · Last modified: 2009/02/26 15:32 by wurzel
Recent changes RSS feed Driven by DokuWiki