Thursday, January 28, 2010

auto Keyword Re-purposed for C++ 0x

In the coming C++ 0x (1x ?) standard, the auto keyword has been re-purposed to behave like the var keywork in C# 3.0. Currently auto is a storage class specifier that almost no one uses. For example, the following C++ statements are equivalent:

auto int x = 5;
int x = 5; //implicitly auto

Being that this is the default, no one uses the auto keyword. So now this use of auto has been removed from C++. It is now a note to the compiler to determine the type of a variable from the usage context. For example:

std::vector<std::string> vs;
std::string str = "foo";
vs.push_back(str);

// we all hate to type this stuff!
std::vector<std::string>::iterator iter = vs.begin();

// now we can just type this:
auto iter = vs.begin();

I can't wait until this is supported in Visual Studio, maybe version 13 in ten years or so...

No comments:

Post a Comment