Reflection on reprint of--c++

Source: Internet
Author: User
Tags lua openstack swift

Reprinted from http://blog.csdn.net/yapian8/article/details/46983319

In the last two years C + + Many people come out in pursuit, and the followers are full of all kinds of superiority, it does not seem to write C + + You are a low-end programmer for a lifetime, in the face of this phenomenon, should not come out in a timely manner of black C + +? Oh ah.

We're going to be a little entertaining, and the number of jokes about C + + is unclear:

Joke: C + + is an unlucky language, it is said that Boeing before using the ADA for aircraft hardware programming, has been using the good, and later recruited a gang of college students, students said I am still in such a backward language, and then replaced by C + + reconstruction after the plane crashed.

Joke: What is a C + + programmer? Is originally 10 lines to write the program, he does not want to use 30 lines to complete, and call themselves "encapsulation", but often to the second project will be 80% break rewrite, and the beauty of its name "refactoring."

Joke: C easy to wipe the gun fire to their feet, with C + + although not easy, but once the fire, will be your whole leg to blow up.

Joke: Two-year-old Java programmers are talking about object-oriented and design patterns, while two-year-old C + + programmers are talking about how the template and various language specifications are going.

Jokes: People who teach C + + earn a lot of money, and many people who really use C + + die badly.

Joke: C + + There are too many places to make a person to express themselves "very smart", so the longer the use of C + + people, about the feeling that they "very smart" results into the trap do not know, fell out of the pit is also estimated that he did not learn C + +.

Joke: A lot of people who wrote more than 10 years of C + + program, so far, it is unclear how many C + + is the norm, still occasionally fall into some pits.

Jokes: Many people who think C + + facilitates cross-platform code find themselves finding two C + + compilers that support the same standard when actually writing cross-platform codes.
—————

Q: Why can C + + see so many fans?
A: In fact, because of windows, because of the rise of Windows led to the c++,c++ is a only suitable for the development of the GUI language.

Q: Why is C + + only suitable for developing GUIs?
A: You see there is no GUI under UNIX, why exclusively C? All system-level problems can be found in C-mature solution, application-level issues can be used in other high-level language to solve, where there is C + + what is it?

Q: You have a logic, there are C + + projects under UNIX.
A: Yes, you can write any bad code in any language.

Q: Don't talk nonsense, what are you talking about? Even C + + and Windows are all pulled together.
A: Think back to the scene of that year, a Daniel teaches a group of beginners how to program. While developing one side pointing to the screen said, you see, this is a button, we can use an object to describe it, it is a panel we can also use an object to describe it, and you have not found that the Button and the panel is a blood relationship, you see ... So it came out .... The following students used to learn the backward school textbooks, and some even used Turboc's BGI library to draw some points and circles. Where have you seen such a gorgeous Windows interface ah. Daniel said, like the golden rule engraved in their own young psychology. While learning Windows, and found that, sure enough, they all need a base class, sure enough, they are brotherly relationships, together contain some basic attributes, can be put into the base class. The more they use it, the more they subconsciously feel that C + + can help them solve all the problems because C + + is so smooth for them to solve so many interface problems. So after the development of the interface, they continue to develop, when they encounter a variety of design problems, but they believe that they do not use good C + +. So he forced himself to use it, and then it was finished.

(click More to expand)

—————
I have a bunch of jokes about C + +, you don't need to be a C + + powder. Anyway, why black C + +? Not black, I have been using C + + since 94 (formerly C and Pascal), all the way to see the growth of C + +, with C + + written code, add up should be more than 10MB, C + + all kinds of treasure I have read, until 2004 began to cut back to C, the main reason is to find a lot of can not use C + + ideas continue to solve the problem, or the use of C + + thinking to solve the problem will be very bad.

At that time (2004-2005) is the C + + flying, the words will be called C + +, with the template, I jump out to say you wake up, do not go overboard, the world is not all abstract data structures and algorithms can be described clearly. So many people excitedly jump out to say: "You do not understand the essence of C + +, you do not use C + +". I asked them: "Language is to solve the problem, if a linguistics for three or four years will often fall ditch, good language?" If you write more than 10 C + + programmers are difficult to grasp, this is good language? " And they said, "The language is dead, and the man is alive."

I remember a domestic C + + Daniel, in order to correct my "wrong point of view", showed me he wrote a very powerful library, I opened a look, poured a sip of air-conditioning, all are. h files. I can only go back to his three words: "You are a man." This was, of course, an extreme example, and the guy finally started to move the. h stuff into. cpp, it's a good thing.

At that time and cloud wind in a company, 2004 new training, he gave the new person to decorate a memory allocator work, when correcting homework, he often looked at the other side, "not enough C + + Ah, you can 100% oop?" "," 1% of C do not stay ". I had a question about C + + in the company's internal mailing list, and most people said, "Do you see how we write 3D engines without C + +?" ”。 I told them: "John Carmack until Quake3 is still using ANSI C, and later because he had to support D3D, instead of C + +." Why can't C write the 3D engine? ”。 They told me: "You see, point, is an object, matrix is an object, so many vector algebra calculation, with C + + arithmetic overload is how wonderful things, three-dimensional world is the object of the world." ”。

It is true that the client GUI, only C + +, graphics engine only C + +, these two are the strongest in C + +, so I did not argue with them, forcing them to admit that C can also be very beautiful to write graphics, and C can write very elegant. I am not idle no matter, why to question people's core values, hehe. That year I was taking over a C + + project, code more than 800KB, each crash will take a long time to locate, the project a large number of dependencies, change a place, before and after a few places, a missing, the whole system is silly. After I started refactoring, I drew two weeks, stripped the core of the performance-sensitive part out of the C implementation (only 200KB of code), and then exported the Python interface, using Python to complete the remainder, the entire script layer code was only 150KB. The whole world is refreshing, the entire C + + Project was the original duration of 2 programmers four months, I am a person to reconstruct the time added up to 1.5 months, and the code volume is less than twice times more than the distance, a variety of strange bugs also sweep and do. I look at the 800KB mess C + + code on the left, and look at the neat 300 KB pure C + + Python on the right, wondering, why did this project start doing this differently?

Cross-language interface

The development of modern projects requires not only higher performance, but also a more powerful language description capability. and C + + is in an awkward place, than the bottom, it is less than C can precisely control memory and hardware, a variety of implicit constructs to keep you from the ground, than the description of the ability, faster than the business development and error location, it is not the Python, Ruby, LUA and other dynamic language, At the same time the east and west are squeezed and eaten at the same point.

Soon, around 2006-2007, the problems of other project groups abusing C + + began to emerge: scripting has been a great success in engineering practice, while some projects are pursuing 100% C + + On the one hand, and the need to export scripts to the interface on the other, they find the problem, I don't know how to get a lot of C + + base libraries and interfaces to Lua.

C's interface has a variety of convenient ways to guide the script, but the whole project by a group of CPP has never been eliminated from the use of the script developed, when they want to go to the CPP class export interface to the script, they designed a set of good system, LUA automatically generated machine code, to call C + + classes, yes, C + + Version of Cffi or cTYPES. He wrote a set of machine code for the class that called the VC, and wrote a set of code generation for the class calling GCC. The CPP, after writing to show off his results, and then he left, the project on the line again and again again the appearance of the problem, and then the cloud to support the project team, this set of interconnected C + + project, this set of large code self-generated system deeply disgusted him. Later, it is well known that the cloud began anti-C + +, advocating a return to C, do not know whether it is related to the project.

So I found an interesting phenomenon, anyone who is good at using scripts to improve engineering efficiency is basically c plus dynamic language to solve most of the problems (except GUI and graphics), and anyone who thinks C + + dominates the universe is a lot of people who have never used scripts or used them to know how to use them.

With this approach, our products compete with competitors, the same function, the same human configuration, competitors with pure C + + to develop March, we have one months to get out, the same time, the opponent can only try the wrong one, we can try the wrong three times. Later, according to the colleagues we recruited to say, competitors are also beginning to gradually reduce the proportion of C + +, increase the proportion of Java, this is a good thing, everyone is making progress.

The embarrassment of ABI

The ABI level C + + interface has never been standardized, the class interface introduces a lot of hidden issues, such as memory problems, a class is instantiated in a library, if you release them in another library there are a lot of problems, because two of dynamic libraries may be different memory management system. You use the allocator here to allocate a piece of memory, and then use the allocator there to release, no problem is strange. A lot of the solution is to add a release method (such as DX), to tell the outside of the people, run out of time not to delete, but to call Release.

It is normal for a project to have a large number of modules isolated into a dynamic library, and a variety of third-party libraries and their own libraries are normal for introducing specific memory management mechanisms for high performance. Many people do not notice that the call to release the place is wrong to write the delete fell into the ditch. The more winners cross-ABI defines many classes of inline methods, resulting in the creation of a variety of implicit constructs and destructors in this library, which are refactored and mess mess. C is much clearer, constructs you to call fopen, destructors you to fclose, does not have any ambiguity. In fact, the contradiction of C + + is to admit that as a system-level language memory management should be given to the user decision, on the one hand, but also define a lot of user-controlled memory operation Behavior . So the C + + standard across the ABI is too late to be defined, not because the polymorphic Abi is complex, but because the language logic is conflicting . To compensate for this contradiction, C + + introduced the operator New,delete, which new/delete overloaded is a patch that does not logically make the language complete, its appearance, and further drag users into the abyss of bugs.

In fact, today we go back to see this problem, can find two basic principles: cross-ABI level on the introduction of an uncontrolled memory mechanism from the language is problematic, can only rely on developers to agree on a variety of smart base class and contract development norms to solve, this problem is not resolved in the language layer Secondly, since you define a variety of implicit constructs and destructors, it should be like Java or dynamic language to take over the memory, do not allow users to customize any memory management methods, rather than as a system in the language to give users the freedom to control, on the one hand and users to seize the control with the user.

Therefore, the object-layer ABI interface is too slow to standardize. And the pure C ABI not only can easily cross the dynamic library can also easily and assembly and all kinds of language fusion, not because C design much better, but c as the system layer language does not have to tube it should not tube of things. When discussing this topic, C + + Daniel began repeating the golden rule to refute me: "Language is only a move, you have to practice the internal strength, you can do no recruit win there are strokes, with the drafting can be when the sword, C + + Although there are many pits, you do not use the design is not so good." I said: It should be in the language layer to solve the good things, because the language logic is not complete, will be a lot of problems to developers to solve the enormous increase in the developer's thinking burden, like a broken house on the table paste. You Jin Yong See more, martial arts again high, when you get a gun found that bullets do not necessarily forward, and occasionally will be shot back, I ask you to focus on the enemy? Or is it time to watch your bullets shoot at yourself?

System-level frustration

The frustration of C + + is to move into the embedded and operating systems so near the hardware layer of things. Everyone felt that the universe-level programming language, naturally capable of all tasks, quickly found several problems:

  • Unable to allocate memory: The original C can be completely independent of memory allocation, code write thousands of lines a malloc not all line. Embedded under the processor power, jump to a specific address (such as starting address 0), the first instruction is generally used in the assembly to write, fixed in 0 address, is simply initialize the stack, and then jump to the C language of the start function, think at this time the memory allocation mechanism has not been established, you define two classes, how to construct it? Most of the time on a microprocessor with limited resources is to operate with a piece of static memory. C + + Write to write cool, a variety of implicit constructs appear, it is silly.
  • The standard library relies on: At the language level,all features of C can be run without relying on any library , which provides a handy feature for writing system-level and cross-platform cross-language code. and C + + will not work, I want to construct ah, I want to be abnormal, why can't you give me powerful running time? What, you still want to use STL? Don't look at how bloated the library is (memory footprint, code size).
  • Exception handling problem: the underlying development requires strict handling of all error returns, this line of calls, and the next line to determine the error. And the exception is a loose error handling, the application layer so write no problem, the system layer so write is very embarrassed. What is the difference between a try and a call to C after each line call? C + + 's constructor is no return value, if the construction of an internal error, you must be forced to catch the exception of the constructor, even if you catch, the construction of the exception of course will automatically trigger the destruction of related internal objects, but there are many non-destructor resources (such as system resources, such as the C interface resources, None of them have a destructor, the whole process is very difficult to control, at this time the instance is a semi-initialized instance, what should you do with it? So someone removes the initialization code from the constructor, constructs a variable only, initializes a new init function with the return, and the code writes a lot more than C redundancy. Not to mention the hardware interrupt occurs, in the case you do not know, colleagues transferred to some third-party library, you do not have the outermost layer of the new exception to catch, this exception where to throw it? When there is not enough memory, you want to throw a outofmemoryexception, but the memory is not enough, at this time completely incapable of constructing this exception to do?
  • Processor compatible: C + + class relies on the address of the base site + offset addresses, many non-Intel series microprocessors only a simple given address addressing, do not support such a statement to implement Base+offset addressing, many C + + code compiled out need more instructions to calculate the address, Result in a lot of performance degradation, outweigh the gains.
  • Implicit operation problem: C is characterized by simple direct, each line of statements you can clearly know what will be translated, the system will strictly follow your code to execute. and in C + +, such as STR1 = str2 + "Hello" + str3; Such a statement, few people really say how many times the structure and copy, such writing code is very irresponsible, the bottom of the need for more granular and strict control, with C language Control Force stronger.

Of course, said a lot of people here and said, "C + + is a superset of C, a specific place you can exactly follow the C's writing to do it." No one's forcing you to construct classes or use exceptions. "Yes, according to Linus:" to write a system-level good portable and efficient code in C + +, and ultimately to use the functionality provided by C itself, which is already perfectly available, so the system layer uses The meaning of C is to exclude the interference of other features of C + + in the language layer . "

A lot of people remember Linus in 2007 because someone asked Git why not use C + + development to bombard a C + +. In fact, when C + + was in the heyday of 2004, someone asked why the Linux kernel was not developed in C + +, and he shelled it once:

In fact, we tried to use C + + in Linux in 1992. It's disgusting, believe me, writing the kernel in C + + is a "BLOODY STUPID idea". In fact, the C + + compiler is not trustworthy, they are worse in 1992, and some basic facts have never changed:

– the entire C + + exception handling system is "fundamentally broken". Especially for writing kernels.
– any language or compiler likes to hide behavior behind you (such as memory allocation) is not a good choice for developing the kernel.
– You can use C to write object-oriented code (such as a file system) without having to write a piece of crap in C + +.

In general, for anyone who wants to develop a kernel in C + +, they are introducing more problems and can't see exactly what they're writing as clearly as C.

C + + fans tried to bring C + + into the system layer at the hottest time in C + +, but never succeeded. So whether it is embedded, or operating system, near the hardware in the development of the bottom, are exclusively C code, there is no place for C + +.

Reflection on Application Layer

When STL comes out, it gives the illusion that C + + can easily develop application layer logic. Because a lot of language layer is not strict things, let STL to patch the way to complete, so many think can write Java like writing C + + beginners fell into a pit. For example List.size (), under Windows VC STL is the length of the list is saved, size () directly O (1) Return the variable, and in the GCC STL, the list length is not saved, size () will search all nodes, O (n) Speed return.

Because the language layer does not support strings, resulting in the std::string implementation is very non-uniform, you copy constructs a string, some implementation is a reference, only use Copy-on-write method reference. Some places are new, some implementation is the use of memory pool, some to implement thread safety, some implementation thread is not safe, you can not tell the same statement after what exactly did what (see Meng Yan "the father of Linux is not rough").

For example, I would like to use hash_map, in order to cross the platform (when you really write cross-platform code, you can hardly decide the target compiler and their version, want to use also can not use unordered_map), I can hardly point out a unique way to declare hash_map, In order to ensure the normal use of hash_map under different compilers, you have to write this:

#ifdef __gnuc__
#ifdef __deprecated
#undef __deprecated
#endif
#include <ext/hash_map>
Namespace Stdext {using namespace __gnu_cxx;}
Namespace __gnu_cxx {
template<> struct hash< std::string > {
size_t operator () (const std::string& x) Const {
return hash< const char* > () (X.c_str ());
}
};
}
#else
#ifndef _msc_ver
#include #elif (_msc_ver < 1300)
#include <map>
#define Ihave_not_hash_map
#else
#include #endif
#endif

#ifdef __gnuc__
using namespace __gnu_cxx;
typedef hash_map<uint32_t, xxxx*> hashxxxx;
#else
using namespace Stdext;
typedef hash_map<uint32_t, xxxx*> hashxxxx;
#endif

If there is a better cross-platform wording, please tell me, I really can't see. A basic container is so hard to use, so many C + + programmers all day thinking about various specifications, there is no time to really think about programming.

Because the language layer to be compatible with C, and do not like C just do the work of the system layer, resulting when C + + in the application layer, can not take over memory management, can not support the language layer string, can not implement the language layer base container. So we need to use some STL and other things to provide convenience, but the STL itself is full of various pits. And does not say that the memory occupies large, the program volume is large and so on, when compiles the speed to be very much. So why is it that everyone in C + + is happy to reinvent the wheel and implement a variety of basic containers and strings, resulting in almost every different C + + project with its own specific string implementation. Just because everyone stepped on the hole, they began to feel the need to control these details. STL's starting point is good, but only simple small program inside casually use, really big project use, STL easy to bring people ditch, so a lot of large C + + projects are their own implementation of a set of things like STL, this is not contrary to the original purpose of STL design?

The lack of language layer, let everyone in order to meet the needs of the rapid iteration of business development, create a lot of very basic design agile base class, to provide similar garbage collection, reference counting, copy-on-write,delegate, and so on countless functions. Each project has a series of basic classes such as Baseobject, which introduces a misunderstanding, two years later you come to see your code, found that some baseobject do not meet the requirements, or you and the other Project merge code, you need to merge some fundamental properties. Graphics and GUI These perpetual models are good, the application of the development of the ever-changing, once these agile base class no longer adapt to the development of the project, often face the cost of comprehensive adjustment.

Open a C + + Daniel Blog, a lot of places to teach you std::string principle, need to pay attention to matters. Map limits, vector principles, teach you how to implement a string. This is called the "mental Burden," which distracts you from the phenomenon that is never seen in other languages. Soldiers do not study how to the front-line kill, every day in pondering the principle of Rob and gun, all the way to think about how to use the gun will not go off, with guns will not explode to yourself, how to fight the war?

So in the years that followed, more and more people began to reflect on the problems of C + + overheating in the previous two years, such as the high-performance network library ZEROMQ author Martin sustrik: "Why do I want to use C instead of C + + to achieve ZEROMQ", such as the cloud wind of the "cloud of Blog:c regression", For example, "why C + + is isn't" back "that caused the hot discussion.

be replaced completely

After 2008, the industry competition is more and more fierce, just as we worry about how to improve the efficiency of development, while falling into the various pits in C + +, more and more application development programs emerge, they can be a good substitute for C + +. Developers from all walks of life have gradually brief encounter to find a variety of better solutions: the need for the bottom control of the pursuit of performance design, everyone back to C, and need to quickly iterate things to find a variety of dynamic language, between performance and development speed, there is Java, it seems like a lot of black Java, the language is insufficient, But better than C + +, not so many pits, really consider object-oriented, really let people put their minds on the design. So black can not block Java on the Tiobe and C language is not the first is the second fact, and then Black also block Java in the cloud computing, the outstanding contribution of the field of distribution.

So after 2005, C + + was in a completely superseded process:

    • underlying system: further regression to the C language, stronger control, more precise operation.
    • Web Development: Around 2006, C + + and fastcgi have been thrown out of the web world together.
    • High-performance services: varnish, Nginx, Redis and other new high-performance network servers are all pure C development.
    • Distributed applications: Around 2007, C + + was completely driven away by Java and other dynamic languages.
    • Game Server: 2008 years later evolved into C and script, completely not see the fat C + + service end.
    • Parallel Computing: 2010 years later, go, Scala, Erlang, and easy to go interface, C is not C + +.
    • game engine: Yes, C + + and scripting, but with more and more open source engines, there is less demand for engine classes these days.
    • Game logic: script
    • Multimedia: SDL Pure C,ffmpeg is the core part of pure C,WEBRTC (DSP, codec) is pure C.
    • Mobile Development: In the early years C + + can also be developed under Symbian, now basically by the Java + OBJC + Swift drove away.
    • Desktop Development: Qt+script, C # and so on can make a beautiful cross-platform interface. And the interface scripting trend, do not need C + +.
    • Web page front end: JavaScript, HTML5, Flash
    • operating system: FreeBSD, Open Solaris, Linux, RTOS, Darwin (OS X bottom), all pure C
    • Virtual Technology: QEMU/KVM (the cornerstone of cloud computing) Pure C,xen pure C
    • database: MySQL (core pure C, peripheral tools C + +), SQLite pure C, postgresql/bdb/unqlite pure C
    • Compiler: C + + co-exist, but compiler with scripting does not matter, I am also on a platform in Java written by C + + compiler
    • Big Data: Kafka, Hadoop, Storm, spark all use JAVA/JVM series technology
    • cloud storage: OpenStack Swift python, HDFs Java, and many more scenarios with Go

As you can see, even though the old lines of C + +, GUI and graphics (and there are some areas in which C + + cannot be replaced in the short term, like COBOL in a trading system), there are more and more challenges these days, such as the newly released rust (how do you see Rust's application prospects?). – Informed by the user's answer). It can be found that the development of technology diversification, with the most appropriate technology to develop the most suitable application is the future trend. It is the C language's strength to write high-performance, controllable public components for these different technologies and interface easily with other languages. So no matter the application layer language is ever-changing, the demand for system-level development language C is still so stable, and in this process, where is the C + + shadow?

Topic Summary

So the future trend is: C x The trend of the mix of languages, from Tiobe on the C + + index of 10 years down three times times can be seen, in the future there will be more technology to replace the remaining C + + solution in the corner, C + + usage will be further reduced. So the main question to learn whether pure C has a future, I think if the master can master the C language proficiency in the left hand, cultivate systematic thinking habits and accurate control of memory and hardware skills; the right hand continues to learn all kinds of new development technology, can deal with the rapid development of various segments, can be ambidextrous when encountering new problems, So the future work must be able to a big step. As for C + +, have time to look at the line, forced to maintain someone else's code in the case of writing two lines.

Story sharing

Ancient use of arrows for long-range attacks, high demand for the shooter, aiming at the difficulty, need to keep the quasi-heart. It takes a long break to open a bow 20 times in battle. The power of the crossbow is far better than the bow, the manufacture of Qin Yu, such as modern automatic rifles are generally precise no two, it can prolong the shooting, but also accurate aiming. The firing speed of the crossbow is more than the number of bows and arrows, and the power is astonishing. Because the operation of the crossbow is very simple and does not require shooting skills, it is easy for civilians to master its use. Qin by the crossbow, in the war has made a lot of advantages, known as the "tigers of the teacher."

When Japan surrendered, the emperor was guilty of sin. Many soldiers do not want to believe that at this time really, find various reasons to refuse to believe. Some soldiers even thought that the emperor's broadcast is the enemy capitulate tricks, so hide in the jungle continue to small groups intelligence, attack can attack targets, waiting for the boss to give them new orders. It was not until a few years later that all the people around were wearing everyday clothes, and the "enemy" who came to patrol the mountains changed from the soldiers to the patrols, and they all felt that it was the enemy's disguise. At the same time, when Germany was defeated, the last SS fought until 1957 to surrender.

—————————————–
A lot of people think Java is slow, C + + fast Java 10 times times more than the past century, modern Java is only 1 time times slower than C + + 70%,c++ even faster than Java. Do not feel that the dynamic language is slow, JavaScript is only 2.7 times times slower than C + +. Luajit is only 5.8 times times slower than C + +. In the development of JIT technology today, C + + in the performance of the gap between the dynamic language/java less and less, ease of use and productivity gaps, but with dynamic language/java more and more large.

—————————
Finally, add a picture:

Reflection on reprint of--c++

Related Article

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.