80 likes | 185 Views
Generic Programming and Library Design. Brian Bartman bbartman@kent.edu. Introduction to C++0X. C++0X is the newest standard of C++. It adds many new features and extensions of the language. Some of the new features are: Type inference Lambda functions Variadic Templates.
E N D
Generic Programming and Library Design Brian Bartman bbartman@kent.edu
Introduction to C++0X • C++0X is the newest standard of C++. • It adds many new features and extensions of the language. • Some of the new features are: • Type inference • Lambda functions • Variadic Templates
Type Inference • Type inference refers to the automatic deduction of the type of an expression in a programming language. • Many programming languages have type inference, for example C# and java. • Type inference in C# allows for the user to specify a variable a var and have the compiler deduce what type the variable actually is.
Type Inference in C++0X • New and re-tooled keywords used for type inference: • auto • decltype()
The auto keyword:Variable type inference • Auto is used to deduce the type of a variable based on the return type of a function being assigned to it. • Example: #include <list> #include <iostream> using namespace std; int main() { list<int> myList; for(auto x = myList.begin(); x != myList.end(); ++x) cout << *x; }
The auto keyword:Function return type inference • Late binding function return types. • Allows for the return type of a function to be declared after the function is declared instead of before like usual. auto foo(list<int> x) -> list<int>::iterator; • There is a special case which allows for one line functions to not to have to give a return type other then auto. auto begin() const { return iterator(memPtr); }
The decltype keyword:Basic Description • decltype is a new keyword added with C++0X. • It is a built in function of the compiler, which is completely evaluated at compile time and yields a type from a given expression. • Because decltype is a built-in function there is no need to include any header files.
The decltype keyword:Usage Examples • The main usage examples of decltype are with functionality of C++ which requires a type. For example template parameters. unsigned int x; double y; list<decltype(x + y)> myList; The above is valid C++0x and will yield a list of doubles.