03 July 2009

Type Inference in C++

A short note to let you know about a long-missed C++ feature you can now use.

Since gcc 4.4 (21 April 2009) you can now skip giving the types of variables that you initialize. Do you remember the old macro

#define foreach(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

? Well, you can now write

#define foreach(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)

In fact, since the definition is shorter now, you might even consider ditching the macro altogether. After all, macros cause bugs that take much time to track down.

Edit: Oh, yeah. I forgot to mention that auto will be a standard C++ feature, while typeof is a GNU extension.

6 comments:

Razzmatazz said...

boost's foreach is even better and works on many types of containers and values: http://www.boost.org/doc/libs/1_35_0/doc/html/foreach.html

rgrig said...

You can probably use "auto" with BOOST_FOREACH too. As for "better", good luck debugging the 3000 characters BOOST_FOREACH expands to plus the 1000 lines in the hpp file if something goes wrong. Of course, you also include a dependency on an external line when one line would suffice in most situations. As a bonus, your compiler will magically become significantly slower.

But, if you understand stuff like C++ static asserts, concepts, and policies, and if you already use some other parts of boost such as the graph library or the smart pointers... then what the heck, BOOST_FOREACH might be good for you.

Anonymous said...

Nothing wrong with the auto usage, but what are you doing using macros in 2009!? Perhaps you did not get the memo in the last century, but macros are evil and should be avoided.

rgrig said...

@Anonymous: "Macros are evil" is in the same category with "goto considered harmful": a mantra that is idiotic when people apply it without thinking.

The really nice Boost libraries use macros extensively. (You might think I'm sarcastic after my previous comment, but I'm not: Boost rocks.) Macros drive about 100% of current research in mathematics and computer science. (TeX uses macros.) There are ways to get rid of most macro induces headaches. And modern programming languages have reallycool macros.

rgrig said...

@Anonymous: A sign you shouldn't use a macro in C++ is that you can't list quickly one way in which the particular macro you are considering will behave strangely. That means you simply aren't good enough to grasp it.

Anonymous said...

I was having an issue condensing some code into something I'd want to type repeatedly across a framework, I tried every variation, and nothing was satisfactory.

Boost hit the same wall with a similar problem, and thus: BOOST_FOREACH was born.

I figured, if it's good enough for Boost, who am I to judge, and now happily type 50+ less characters each time I use the code in question.

Evil, maybe, but, I'd rather be evil than driven insane by excess typing.

I think as long as you're using macros for shorthand, it's okay, you're basically replacing something long, with something easier to type, it's a bit like making a snippet, only more accessible for tweaking, etc,. (Not to mention, that it 'visually' produces less code, making things look neater than had you used a snippet for the same job.)

Example:

for (int i=0; i < 1000; ++i)
{
// Code Here
}

vs.

FOR(i, 1000)
{
// Code Here
}

A macro is simply a directive which says, replace what I wrote with X code, so I'm using macros in their purest form, if ya think about it. ;)

Post a Comment

Note: (1) You need to have third-party cookies enabled in order to comment on Blogger. (2) Better to copy your comment before hitting publish/preview. Blogger sometimes eats comments on the first try, but the second works. Crazy Blogger.