New C ++ 0x standard: Welcome or reject?

Source: Internet
Author: User

Today, I saw an article on csdn and found that C ++ 0x (or C ++ 11 or C ++ 0b) has many exciting new features,
I have searched and read the C ++ 11faq of stroustrup, so I hope to share it with you. Everyone must have their own
.

1. What do you like or dislike about the new features?

Below I would like to show you a few new features that are currently available, giving readers who don't want to take the time to learn more.
(When Using mingw GCC 4.6.1, add -- STD = C ++ 0x or -- STD = GNU ++ 0x)

In C ++ 98, I want to loop through a vector <string>:

1 for(vector<string>::const_iterator itr = aVec.begin(); itr != aVec.end(); ++itr){
2   cout << *itr << endl;
3 }

In the new standard, this will do:

1 for(auto e : aVec){ cout << e << endl;}

Two features are shown here: the new auto keyword and ranged. The semantics of auto in the new standard has changed and become
The compiler automatically derives the meaning of the type. The original auto keyword has almost no effect, and the local stack variable is
By default, the use of auto is hardly seen. In the new standard, errors are generated when the original semantics is used! Ranged for also
It is easy to understand, that is, to learn the for loop of Java, C #, and other languages. You can even do this:

1 for(auto i : {1, 2, 3, 4, 5}) {cout << i << endl;}

So the new Code should have seen a lot of auto. This is not a dynamic variable, but it simplifies the tedious work and enables the Compiler
Perform low-level repetitive tasks. Three Modern C ++ types are shown below:

1 Map <string, vector <int> * mymap = New Map <string, vector <int>; // oldest Style
2 shared_ptr <Map <string, vector <int> mymap = New Map <string, vector <int>; // Old Style
4 auto mymap = make_shared <Map <string, vector <int> (); // New Style

Make_shared is a library function that can also be used in vc2010. It is a very convenient function to replace New and directly returns
Create the shared_ptr of the object. The new standard recommends explicit use of smart pointers to completely eliminate Delete. For example, express the unique ownership relationship
The unique_ptr is used to express the sharing relationship. The shared_ptr is used to express the weak reference. Only the weak_ptr is used to express the non-having relationship,
* Is used only for the purpose of recording an object *. There is no need to worry about the right angle brackets of the template.

Another small change is worth noting, that is, class member initialization. You know that in Java or C #, variables can be directly started.
The statement must be made at the time of declaration, and C ++ must be included in the initial list, but the new standard has also been improved. So let's look at the following code:

1 class A
2 {
3 static const int a = 3; // Ok in 89
4 static int a = 3; // Error: not const in 89
5 const int a = 3; // Error: not static in 89
6 int a = 3; // Error in 89 but Ok in 0x
7 public:
8 A():a(0){} // Ok in 89 but not necessary in 0x
9 }

C ++ 0x has a major change and added a language-level Lambda function. And you can use function pointers or function objects (so-called functor)
Use the following syntax to declare an anonymous function:

1 [](int a, int b){return a>b;}

This is equivalent to integrating the boost Lambda function library into the language. There are several advantages: first, there is no external dependency,
Second, it may reduce the size or efficiency of the Code caused by the template. In general, it is a good function. In the future, C ++
The interface library can write event handler in a similar way.

In addition to some column improvements in language, the standard library has added a lot of content. The most significant ones are thread and RegEx.
Greatly expands C ++ capabilities (based on cross-platform. For more information, see here.

2. c ++ 0x, a new language?

You may have read many articles related to c ++ 0x, or you just read my brief introduction above.
What attitude does C ++ have in its evolution? I see complaints in csdn and other places. Many cainiao think it is too complicated,
It is difficult to get started. C ++ his father thinks C ++ 0x is a new language. To some extent, it is true.

I also have limited experience in C ++. I personally feel that C ++ has a mixed development experience. For C ++ as C with classes
For most of the older C ++ libraries that use C ++, there is really a strange way of writing code in a new language.
Sense. I have seen a lot of code that uses templates. The only feeling is that there is no need for complexity or language.
The incompetence of itself can only be achieved through various tricks.

The C ++ mode is too broad. If it is the underlying code, it can be well done by adding several classes to C; if it is an application
One feature that is badly lacking in development and C ++ is garbage collection. To improve efficiency and reduce memory errors, it is based on templates.
Smart pointers are designed to improve ease of use in a language that is not suitable for application layer development.
No loss of efficiency, the consequence is unnecessary complexity. For example, for type operations, large frameworks all have reflection mechanisms. c ++ must
To a certain extent, there is a loss of efficiency, so there are various types of operation templates. C ++ templates make the best use of the resources,
It was developed to a non-compound degree. As a result, using the template program, the volume quickly expands and the error information is basically
Unreadable, greatly increasing debugging difficulty, and steep language learning curve.

The new C ++ standard aims to improve the ease-of-use of the language. A series of changes are largely syntactic improvements,
In fact, there are few essential semantic changes. The other is to add all the mature libraries in boost (or integrate them into the language ).
), So the modern version of C ++ is released. Therefore, C ++ 0x is not a new language.

3. view the positioning of C ++ language from the new standard

C ++ is based on the C language and compatible with it, so that for application development, its code is too low-level and development efficiency
Low. Modern compilation languages, such as C # or Java, hide some underlying features of C ++, while absorbing
Some of its high-level expressions, coupled with powerful class library support, greatly improve their ease of use. If you use QT or
WxWidgets and C ++ can also easily complete some application development, but the final program is not necessarily
Has a great advantage in efficiency or size (interested readers can compare codeblocks and sharpdevelop)
Memory and speed of two IDES ).

So how can c ++ locate it? C ++ 0x seems to be developing towards application development because it tries
Smart pointers hide the trouble of C ++ memory management. templates and classes are used to implement various advanced semantics, so that you can directly
Map the problem to the language structure. The standard library also provides Unicode, threading, atomic operation, and standard
Container, regular expression, and so on provide comprehensive support. I want to add the boost filesystem.
To implement a cross-platform STD: Ui, C ++ can actually sit with Java or C # (or self-Downloading identity ?)

As a system-oriented programming language, I feel that advanced semantics such as generics and Lambda are the future direction.
The positioning of C ++ is a bit complicated (and confusing ).

In fact, the underlying development, C ++ as a better C is completely worth advocating. The features used for playing games are
To simplify some of C's tedious operations, because many c ++ features will reduce efficiency. Interface Creation
Application Development and many features of C ++, compared with the large platform (. NET or Java) built by a large company, whether it is optimization,
Language Features, ease of use, and class library support are not comparable. Sometimes I feel that one of the advantages of C ++ is
You do not need to force the user to install the support platform, but now with the popularity of window7,. Net platforms are installed there by default,
There is no reason to leave such great resources out of use, but to use C ++ to do thankless things. Python and other dynamic
Language. With the upgrade of hardware, its efficiency disadvantage gradually disappears. The development tool recommended by Ubuntu is Python +
Pygtk, the development speed and experience, is also very good, because like. Net in windows, Python now
It is basically the default installation language for all modern Linux releases. You may not be able to find GCC, but you must be able to use
Python. As a language, Python has absolute advantages in all aspects.

In this case, if C ++ does not make a major change in the language itself (into a new language), it wants to use various methods.
To provide high-level semantics, it can only cure the problem, and is always not suitable for the so-called high-level development (relative to the underlying ).

4. Are you welcome or not?

Now C ++ 11 is a standard, but most compilers are not fully implemented yet. Of course we have no choice
Rights, but C ++ is backward compatible, so we can choose whether to use the new function.

I still like C ++ very much. For some simplified programming improvements, I really agree with both hands. I will open it all the time.
-- STD = C ++ 0x option to use all the new features that can be used. If this is C ++, I will use C ++,
If I write C, I will only use the C function and compile it into a C module. The ABI compatibility is also good. Feel now
C ++ and C are a bit like dual personalities. They need to be divided into two (actually different) languages and then develop each other.

In addition, the standard I/O Library of C ++ is still cautious (especially with GCC), and sometimes some inexplicable problems may occur,
It is safer to use C's stdio. I personally think that the printf format is easier to use.

These improvements may not be good for beginners, because if beginners learn C ++ directly (just like C ++ dad,
Directly use C ++ as a new language. Only C,
Or I have learned the C subset of C ++ before I can understand it. For example, why are so many inexplicable pointers. Because
C ++ is essentially a C system model.

C ++ veteran may think that these things are nothing, but they are fixed by the Programming habits that have been formed for a long time. I even
Hopefully, in the future, all pointers to C ++ will be smart pointers by default. This saves a lot of trouble, but it breaks the compatibility with C.

Therefore, if the language nature of C ++ has not changed, it is welcome to reduce the pain points.
The development of language itself has little significance. If C ++ can generate a new language, it is expected to become a language that is not on a virtual machine.
A big and Comprehensive Development Platform is another thing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.