We are building a project using C++ 11. The features of C++11 is quite surprising. However, it's so disappointing that Eden does not support C++11. I am learning it now and there are some features in this issue.
C++ 11 Catalog (which I think they are important)
Rvalue references and move constructors
constexpr – Generalized constant expressions
Modification to the definition of plain old data
Extern template
Initializer lists
Uniform initialization
Type inference(auto)
Range-based for loop
Lambda functions and expressions
Multithreading memory model
Keyword : auto
If you have used python or swift of javascript or other 'loose data type' language, the compiler or interpreter will deduction the type of the data automatically. For example in python:
a = 100 # integer type
b = 100.0 # float type
c = "abcde" # string
d = open("./x.txt", "r") # file handler
However, on the contrary, we know that, c++ is a kind of 'strict' type language which means that the compiler request datatype when you declare the variable. It's in C++ 11 that key word 'auto' make it awesome for auto data types.
auto a = "123";
auto b = 10.0;
auto c = {1, 2};
The compiler of C++11 standard can deduction the data type automatically, too. You can try it yourself. And we can get type information using the following example:
#include <typeinfo>
#include <iostream>
using namespace std;
int main() {
auto x = 1.0;
auto y = "abcde";
auto z = {1,2,3,4};
cout << typeid(x).name() << endl;
cout << typeid(y).name() << endl;
cout << typeid(z).name() << endl;
return 0;
}
typeid is a C++ language operator which returns type identification information at run time. It basically returns a type_info object, which is equality-comparable with other type_info objects.
Lambda Function
Also, in some language, especially in javascript, lambda function is very common.
x = function (a, b){return a+b};
x(1,2);
In C++11, lambda function is provided and it's very useful to replace annoying function pointers.
We are building a project using C++ 11. The features of C++11 is quite surprising. However, it's so disappointing that Eden does not support C++11. I am learning it now and there are some features in this issue.
C++ 11 Catalog (which I think they are important)
Keyword : auto
If you have used python or swift of javascript or other 'loose data type' language, the compiler or interpreter will deduction the type of the data automatically. For example in python:
However, on the contrary, we know that, c++ is a kind of 'strict' type language which means that the compiler request datatype when you declare the variable. It's in C++ 11 that key word 'auto' make it awesome for auto data types.
The compiler of C++11 standard can deduction the data type automatically, too. You can try it yourself. And we can get type information using the following example:
typeid is a C++ language operator which returns type identification information at run time. It basically returns a type_info object, which is equality-comparable with other type_info objects.
Lambda Function
Also, in some language, especially in javascript, lambda function is very common.
In C++11, lambda function is provided and it's very useful to replace annoying function pointers.
Example:
Output:
Well, there is also a equal one:
What is the answer of this? :100: :smile:
We will get error when we use auto in parameters in C++11, but it work in C++14:
unique_ptr (智能指针,无须程序员手动delete堆内存,在恰当时机自动delete)
We will get into trouble if we have the following code:
Oh! Memory leak will occur if we write this code. But unique_pointer helps us to manage memory automatically.
You can test it using 'valgrind', but a mismatched error will still reported with no memory leak.
Range Base Loop
quite convenient to use this kind of for loop somewhere
Compiler
Remember to add -std=c++11 or -std=c++14 or -std=c++15 when you are trying to compile the code.
If you have any question, idea or some experience for learning c++11 - c++15, you can make an issue or comment here.
Hope you learn something.
Happy coding.