Browse DevX
Sign up for e-mail newsletters from DevX


Clean Up Function Syntax Mess with decltype

The funky syntax of a function's return type isn't just unreadable in many cases—it's also a common source of maintenance problems. However, with the new decltype operator your functions will look much cleaner and easier to maintain.


he auto keyword presented here is related to another C++09 proposal: the decltype operator. decltype(e) retrieves the type of the expression e. By combining decltype and auto, you can simplify complex function declarations and reduce maintenance costs. The following sections will show you how to use decltypeto make your functions sing.

Your function consists of a simple return statement. Alas, the manual declaration of a complex return type makes your code brittle and illegible.

Use the decltypeoperator to extract the return type from an expression instead of spelling it out explicitly.

Thanks for your registration, follow us on our social networks to keep up-to-date