I haven’t had much chance to play with C++11, though I’m supposed to be a C++ expert. It’s just hard to use it in a project when not every platform has the compiler support. However, with gcc making such progress on C++11 support, it feels like my projects (glibmm, gtkmm, glom) could switch to C++ only some time soon.
So I’m playing with some of the most obvious new features in the glibmm example code, in a branch. Here are some little commits to demonstrate:
- Use the auto keyword.
I like using this to avoid even caring what type is used, which is just a personal code-style preference. However, now I see that it isn’t quite as useful as I’d hoped. It doesn’t help when the variable must be declared separately from its assignment, for instance where you need to do the assignment in try/catch block. - Use range-based for loops: for(auto thing : things) {}.
- Use >> instead of > > for use of templated types.
- Use nullptr (note that this has no std:: prefix because it’s a keyword).
For the auto thing, I think you should be able to use decltype:
decltype(som_method_throwing_an_exception) var;
try {
var = some_method_throwing_an_exception(…);
….
} catch (….)
…
I didn’t try it, but I think it might be worth trying.
It’s not as bried as using “auto” and assign directly, though.
Thanks. I can see how using decltype(the_function_i_will_use_in_the_try) is useful for generic template code, but it doesn’t seem to make the code much nicer in normal code.