Page Collection for ^2011-06

2011-06-17 CPlusPlus0X

More on type inference

Together with auto there is a new keyword decltype. It is used to determine the type of an expression in compile time. You can do like this:

auto var1 = 3.14;
decltype(var1) var2;

var2 will get the same type as var1. Who in it its turn got its type from the initialization with 3.14 and so they are both doubles.

Right now it might not be entirely obvious what this is good for, and it is probably of most use for library developers, but now you have seen it anyway.

2011-06-23 Computers

Resetting keyboard due to shift and control key problems caused by vmware

Got bitten by this problem once again but it was so long I had forgotten the cure! Had to Google it again. This is how to reset your keyboard under X.

setxkbmap

2011-06-27 CPlusPlus0X

Initializer lists

How many times have you not wanted to easily initialize a container with a list of values. Instead you would have to solve that with repeated push_backs either inline or in a loop over a good old C array that could be initialized with the brace notation. C++2011 now fixes this by introducing the initializer_list-template.

It works like this. Constructors and a other functions can take a parameter of type std::intitializer_list:

void function(initializer_list<string> list) 
{ 
   cout << "Number of args are " << list.size() << endl; 
   for (auto it = list.begin(); it != list.end(); ++it) 
   { 
      cout << "List contains: " << (*it) << endl; 
   } 
} 

You can call function like this:

   function({"hi", "ho", "foo", "bar"});

The standard library classes implements this so we can now initialize a vector of strings with either of these ways:

   vector<string> v1 = {"hi", "ho", "foo", "bar"}; 
   vector<string> v2 {"hi", "ho", "foo", "bar"}; 

So this adds yet another feature that makes C++ just a little more convenient to use, easier on the typing and makes the code cleaner and easier to understand.

2011-06-28 CPlusPlus0X

Taking initialization one step further - Uniform Initialization

Initializing containers is fine but what about structs or classes. Well in good old C++ some initialization was possible using the brace notation. In C++2011 that is taken further by making the initialization uniform and work on any object. So if you have a class with a constructor

class Person
{
public:
  Person(const string & name, int age)
  : name_{name}, age_{age} {};
...
private:
  string name_;
  int age_;
};

you can initialize it like this:

Person me{"No Name", 50};

The same syntax as for a vector or a list but this time for class with a constructor taking two arguments, a string and an int. This works for structs with no constructor as well just initializing each member as you would expect.

You can even do this. Note how the return statement just contains the initialization of the Person type.

Person getPerson()
{
  return {"Foo Bar", 42};
}

2011-06-29 Computers

vmware problems with mouse interaction solved!?

I have had some problems for a while with the mouse interaction on my vmware virtual machine running XP. The problem was that the mouse lost its binding to the virtual machine making it impossible to use the mouse. This is not the way to use Windows I must say! It is possible to crawl for a while but really working under these conditions are not really efficient to say the least.

Googling yet again came up this time with a workaround that really works:

GDK_NATIVE_WINDOWS=true vmware

The alternative using VMWARE_USE_SHIPPED_GTK="force", that I think I have used at times before, didn't cut it this time. vmware dumps core using that.