📜 ⬆️ ⬇️

Interview with Björn Stroustrup about C ++

image
A few hours remained until New Year 2014, in which, among other things, all of us were promised a new standard C ++ 14. However, it will not be a large independent update, but only a refinement of C ++ 11, a bug fix that will give the current version of the language a complete look. Against this background, William Wong (from William Wong ) from electronicdesign.com has interviewed Bjarne Straustrup (dat. Bjarne Stroustrup ), the creator of C ++. The conversation touched on several topics: from the history of C ++ development and the features of the C ++ 11 standard to the problem of learning this programming language.

Some of the terms and concepts from this interview I previously met exclusively in the English version (for example, the word embedded in the context of IT), and I was not always able to find a generally accepted translation in which I would not be sure myself. In these and other ambiguous cases, I indicated the English version of the term in brackets or left it completely untranslated.


')
At the end of the article are links and my notes. The owners of this resource kindly allowed me to translate this interview and publish the translation in Habré, but insisted that I specify the source in a certain way. Do not blame me. Immediately after the link is the actual translation.

Electronic Design, October 29, 2013, electronicdesign.com/dev-tools/interview-bjarne-stroustrup-discusses-c


To date, the programming languages ​​C and C ++ are the most popular in the field of embedded (embedded) systems, as well as in the tasks of building platforms for third-party applications. The author of C ++ is Bjorn Straustrup. He still takes an active part in developing the standards of this programming language, including the latter - C ++ 11. He wrote a lot about this language in his books, for example, in “Programming: Principles and Practice using C ++” . Björn Straustrup has kindly agreed to answer a few questions about the C ++ language itself and its design.

How did you come to develop C ++?

I worked on a project that would allow the Unix kernel to be split into several parts, executed by a multiprocessor or a high-performance local network. And I needed a tool that would allow working with the hardware, providing good performance for system programming tasks, and could also be used to work on a system with a complex architecture. However, at that time (1979-1980), none of the existing programming languages ​​did not satisfy all three conditions at once. So I decided to add to C the concept of classes - like the one that was in Simula. At first, I implemented checks and transformations of function arguments (later they became function prototypes), constructors, destructors, as well as the simplest inheritance. The first version of the C ++ language was called “C with Classes”. By the way, it is curious that for the simplest support of generic programming, it used macros. Later I realized that this approach does not provide proper scalability, and instead of macros I added templates.

I improved the architecture and implementation of the C ++ language over the next few years, until its commercial release in 1985. At that time, the performance and speed of accessing the hardware were very important characteristics, just like today. I found it necessary to implement in C ++ all the features of the C language, and to make them equally effective. For example, in the early stages I found that the structures used to implement the copy constructor occupied 3% more memory than in C. I decided that this should not be so, and by the end of the week I fixed everything. So that programmers do not have to abandon classes without losing any CPU time, inline functions were also added. In general, I was sure that the tools used should be not only expressive, but also effective enough to be used in the applications with the highest requirements.

What did you strive for when developing the C ++ language?

In addition, C ++ could work with the hardware at least as effectively as C did. In addition, the concept of abstractions seemed to me to be very important, which would allow programmers to express their wildest ideas without any time or memory leaks, which they would most likely encounter in self-realization.

This requirement entails the use of strong static typing , as opposed to weak typing in C.

C ++ language was developed for people who are engaged in programming seriously, that is, for professionals in their field. It can be used - and is used - by newbies, but often this leads to different misunderstandings and complaints that not everyone can learn to program in C ++ and that there are things that are very difficult to implement in this language. Of course, there is no universal programming language for everything and everyone, C ++ was never created like this. However, this language is very effective in those areas for which it was developed, such as system programming or programming programs with severe limitations on computer resources. C ++ has no equal where its power is really needed, and I don’t care much that you can write a simple JavaScript or Ruby web application instead. C ++ is not inherently intended for solving problems of medium complexity, with weak requirements for program performance and reliability, as well as it is not intended for use by not very experienced programmers with moderate development skills. Of course, it can be used in such conditions and today it is widely practiced, but there are many other programming languages ​​that would be suitable for this much better.

About the key principles that I adhered to when developing C ++, I spoke in my book “The Design and Implementation of C ++” and in two articles written for the conference “History Of Programming Languages”. In short, I set the following goals:



If in general, C ++ is designed to help in writing high-quality program code. Professional programmers in real life are faced with complex tasks, and this programming language greatly simplifies their lives.

Although, of course, it will not be possible to achieve all these goals at once, and C ++ is not perfect. However, despite countless attempts to create a language instead, C ++, with its strict design, remains the best solution for a wide variety of practical tasks.

You have been involved in the development of the C ++ standard from the very beginning. Has it changed much over time? Who develops new standards?


It is hard to say. The development of a formal standard is a very difficult and, as a rule, dreary business. They are engaged in people with great experience, but they are all specialists in completely different areas of programming, and each has its own vision of the standard. So it can be difficult and time consuming to come to a common opinion, but this is necessary: ​​it’s impossible to satisfy all the requirements at once, and it is impossible to force programmers to use fundamentally new tools. Progress occurs only when supplements are included in the standard, the importance of which is generally recognized. You can not take part in the committee and at the same time constantly get hung up on the little things. You need to be able to see the whole picture and come to a common opinion with the rest. According to my calculations, the committee includes about a hundred organizations and, maybe, more than three hundred actual developers. This is two to three times more than before. Only at the last meetings attended by about a hundred people.

In 2014, we plan to release a new standard, C ++ 14. It will contain minimal innovations, as well as a few corrections, the majority of the committee has already voted for their necessity. I expect that in 2014 everyone will already use C ++ 14, and after that we plan to release C ++ 17 in 2017. But this update will be much more significant, so it’s difficult to judge the timing.

The ISO C ++ Standards Development Committee itself does not have any resources, be it money or full-time developers. It is completely based on the means of its members. For example, to be a member of the committee, they pay $ 1,200 annually. Anyone can declare that, they say, in C ++ there is no normal library for creating graphical interfaces or normal support for task parallelism. Yes, we know. Than to complain, it would be better to help bring these tasks to mind. We have very few application programmers, and often it turns out that innovations are created in favor of the interests of one particular developer.

Many programmers in the development of embedded systems prefer to use C, because it is simpler than C ++, and is more suitable for developing for hardware. Is C ++ complexity really a stumbling block for developing embedded systems?


Not at all. If you adhere to C-style programming, C ++ will be just as easy as C, and it is also suitable for developing for hardware. And surely C ++ is much more efficient than C. I have never seen such a program in C ++, which could be rewritten in C so that it will have less code, it will be more productive, it will be better accompanied - in general, it will be more efficient . I do not believe that this is possible.

The myth that “C is better than C ++” confuses so many novice programmers. So, for example, when they encounter problems, they constantly try to invent something and use completely non-trivial things, rather than using simple and powerful tools. In the end, they get a very complex and confusing code, which, due to their delusions, they take for reference. This whole situation just amazes me. If a person takes on something, and he is constantly told that it is very difficult and useless, then he will not succeed in anything. The only intelligible reason that, as I know, use pure C, not C ++, is the limited capabilities of a particular platform.

However, students and generally newcomers to learning C ++ cannot be blamed, because their mistakes often originate in the process of mastering a university programming course. One day, ten years ago, I happened to lead him among freshmen. I looked into the textbooks - and I was just amazed: instead of clear and easy-to-use C ++ constructions, the books at the beginning considered a bunch of different, unobvious trivia of the C language, and the C ++ tools were presented as something very complicated. This did not discourage only those who wanted to seriously engage in programming.

Seriously, tell me: are vectors from the standard library more complicated than arrays from C? Or, for example, why are students being taught the qsort() function, although sort() both more efficient and more versatile? C ++ has more strict typing than C, due to which object code is processed faster.

Even in textbooks, C ++ is often described as a failed attempt to create a pure object-oriented programming language. Such a statement is usually illustrated by a whole sheet of code in which virtually the entire architecture is broken down into an intricate hierarchy of classes inherited from each other. The result is a completely unrelated character for C ++. Such code is more like a Java program, and, sadly, it usually works slowly.

I also do not like C ++ the way the authors of those textbooks represent it. In response, I wrote my book for students and self-taught teachers - “Programming: Principles and Practice using C ++”. For her study of programming experience is not required, but it caused interest among experienced developers. Only if you just need a review of C ++ 11, then this book will be quite large. For this purpose, I would recommend the book A Tour of C ++. It describes all the key points of ISO C ++ and the standard library of just 180 pages. The C ++ 11 standard is fully supported by the Clang and GCC compilers, partly by Microsoft C ++ and many others, although I'm afraid it may not work correctly on less popular platforms.


In C ++ 11, there were many innovations, including lambda expressions and support for multi-threaded programming. Do you think they were in demand?


To work with streams, I constantly had to use third-party libraries. They were good, but for the last fifteen years I have wanted to add support for flows to the standard, which we have finally achieved. From the point of view of parallel programming, the key innovations of C ++ 11 consist in the organization of memory (which, by the way, was borrowed for the C language), as well as the portability of multi-threaded programs. However, if you are programming at the level of flows and barriers, then the most important for you may be type safety (type safety): no more macros or void** are required to separate and transfer data between threads. However, for someone, tools for lock-free programming are also important.

As for lambda expressions, I have been working for about ten years on implementing them in C ++, which would be more beneficial than harmful. Third-party libraries typically suffer performance. We also managed to achieve lambda expressions of performance comparable to the for loop. I will give an example:

 double sum = 0; for (int i=0; i<v.size(); ++i) sum += v[i]; // array style 


 double sum = 0; for (auto p = v.begin(); p!=v.end(); ++p) sum += *p; // pointer style 


 double sum = 0; for_each(v.begin(),v.end(), [&](double d) { sum += d; }); // algorithm style 


These three code points do the same thing and work with the same performance. You can choose between them based on considerations of programming style, maintainability, and so on.

I would use lambda expressions in just a few cases. In this, for example:

 sort(v, [](const string& a, const string& b) { return a>b; }); // sort in reverse order 


Lambda expressions are a new and quite powerful tool. The catch is that programmers like to use such things literally everywhere, until they understand how this will turn out in the future. For example, it seems to me that it is better to declare the function and functional objects separately. The desired operation will then have its own name, which can be easily called from different parts of the program.

Also lambda expressions open up a lot of room for writing code. This is probably not the place to teach you C ++ 11, but let me give you one example:

 template<typename C, typename V> vector<Value_type<C>*> find_all(C& cont, V v) // find all occurrences of v in cont { vector<Value_type<C>*> res; for (auto& x : cont) if (x==v) res.push_back(&x); return res; } 


In this code, I applied several new things to C ++. The for loop, for example, here reads “for all x from cont ” and makes it easier to cont through the container cont . The auto& x declaration indicates that x should be a reference to the type of elements of the initializing container, in this case the type of elements cont . This loop collects the addresses of all occurrences of v in cont and adds them to the vector of res pointers. So these constructs are no more than syntactic sugar, although they are very convenient.


The essential innovation lies in return : note that I returned the vector by value. In C ++ 98, this return statement would create a copy of res , but in fact it can be large and consist of thousands of elements. In terms of performance, this would be very imprudent. And in C ++ 11, vectors have a so-called move constructor , which instead of copying, “borrows” the res representation (in essence, only three pointers) for use at the place where the find_all() function is find_all() , and the vector itself leaves empty. After doing a return we can never use res again. Thus, returning a vector by value will cost a maximum of six assignments, regardless of the size of the vector.

Displacement constructors are a fairly simple tool. It is available to every programmer, and, moreover, is implemented in all container classes of the standard library. This allows you to easily return large objects from functions and not to break your head once more over memory management.

You can find_all() function as follows:

 void test() { string m {"Mary had a little lamb"}; for (const auto p : find_all(m,'a')) // p is a char* if (*p!='a') cerr << "string bug!\n"; vector<string> v {"Mary”, “lamb", “Mary”, “mary”, “wolf”}; for (const auto p : find_all(v,”Mary”)) // p is a string* if (*p!=”Mary”) cerr << "vector<string> bug!\n"; } 


Try to write the same code without applying the templates and innovations of C ++ 11 and compare the results.


On this subject, I recommend reading “A Tour of C ++”, for details, refer to the fourth edition of the book “The C ++ Programming Language” .

What are some common mistakes that modern C ++ programmers have?

For some reason, they often think that they have to choose between effective and elegant code. They either limit themselves to the low-level capabilities of the language (for the sake of "efficiency"), or they build a huge architecture "for all occasions" (assuming such code is very elegant). In my opinion, the ideal is a combination of maximum efficiency and the most optimal architecture. This happens when the solution of the programmer in the most optimal way satisfies the conditions of the problem, but this, of course, is not always possible. It is rarely possible to achieve this at the first attempt, but it happens quite often, as far as possible for the ideal.

Before giving up on C ++ features like classes or templates, first try to consider their basic use and practice. A reasonable choice will lead you much further than the steps taken at random. Don't commit yourself to building huge class hierarchies or writing intricate meta-code with templates: some of the most powerful features of C ++ are very simple. The best way to write effective code is to not complicate it unnecessarily.

What do you like to do in your free time?

I am interested to travel to different places. I also like to go jogging. I also take a great interest in photography, I like listening to music and reading - artistic and historical literature. I try to spend time with my family and friends. Of course, programming also sometimes gives me a lot of fun, but the question must not be about work. I like to do research, build complex software systems. As someone said, "I can not believe that I still pay for it!"

Translator's notes


  1. "Programming. Principles and practice of using C ++ "in the translation of Dmitry Klyushin, publishing house" Williams. " backwards
  2. About typing, see the article Literacy on typing in programming languages . backwards
  3. Probably, here we still mean the book “The Design and Evolution of C ++” - “Design and Evolution of C ++” in the translation of the publishing house “Peter”. backwards
  4. The C ++ 11 memory model is discussed in detail in the Lock-free data structure article . Basics: Memory model . backwards
  5. A brief overview of move semantics in C ++ 11 is provided in the article Move semantics in C ++ 11 and STL containers . backwards
  6. In general, the key innovations of C ++ 11 (including the aforementioned) are highlighted in the article Ten C ++ 11 Features that every C ++ developer should use .
  7. In the Russian translation of Nikolai Martynov, the publishing house Binom, the book is called the C ++ Programming Language, but I could only find the re-release of the bearded, irrelevant edition of 2001. I did not find any other translations at all. backwards

Source: https://habr.com/ru/post/207894/


All Articles