30 秒でわかる
Stroustrup describes concepts as compile-time predicates that state what a generic component requires of its parameters, such as what it means for something to be sortable. He says he wanted templates to be flexible, as fast as hand-written code and properly type-checked, and that earlier attempts at concepts failed because they did not fit C++ or its performance demands.
- 1Stroustrup says concepts are compile-time predicates that ask a type whether it has the properties a generic component needs, such as a begin and end, random access and a less-than operator.
- 2Concepts are not science fiction: working versions have existed for four or five years, they are part of C++20 and its standard library, and major compilers support them.
- 3The term comes from Alexander Stepanov, the father of generic programming, who called them concepts because they are the fundamental concepts of an area.
- 4Stroustrup wanted templates to be flexible, as fast as hand-written code, and properly type-checked, and for decades he and others did not know how to get all three at once.
- 5The earlier attempt around 2003 stumbled because it did not handle implicit conversions and mixed-type arithmetic well, compiled slowly, and could not meet C++'s performance requirements without optimizers beyond the state of the art.
チャプターごとに全文を読む
Concepts are compile-time predicates, and you can use them today
Stroustrup starts by stressing that concepts are real: they did not exist ten years ago, but versions that work have been around for four or five years. The design came from Gabriel Dos Reis and himself, who were professors and postdocs in Texas at the time. It is part of C++20 along with the standard library components that use it, and it is available in clang and GCC, with Microsoft expected to follow as C++20 arrives in all major compilers.
He explains the basic idea with a sort function. Sorting needs at least two parameters: a data structure with a given type and a comparison criterion, and the two are related, since you can't compare things without knowing their type. To say something is sortable, you look up the standard's definition: it has to be a sequence with a beginning and an end, offer random access, and have element types that can be compared.
Concepts, he says, are compile-time predicates that ask exactly those questions. Are you a sequence, with a begin and an end? Are you a random-access sequence, with subscripting and addition? Does your element type have a less-than? Instead of accepting a parameter of any type, the function says it takes something sortable. The comparison criterion can be less-than, greater-than or something you invent, and it is written to operate on the element type.
The word comes from Stepanov, and C already had the idea informally
Stroustrup notes that the word concept was used by Alexander Stepanov, whom he calls the father of generic programming in the context of C++. Stepanov called them concepts because they are the fundamental concepts of an area, and so they should carry that name.
The idea itself has always existed, he argues. If you look at the K&R book on C, it describes arithmetic types and integral types, lists what they are and states their properties. What is new in C++ is that you can write a concept that asks a type whether it has the properties necessary to be integral, for instance whether it supports operations like multiply and divide.
Getting flexibility, speed and type checking together took decades
Lex Fridman asks why concepts did not arrive in C++11 and what made the idea so hard, going back to templates in the late 1980s. Stroustrup says he wanted three things from templates. They had to be flexible enough to express things he could not imagine, because he disliked languages that force you into only what the designer thought good. They also had to run as fast as or faster than hand-written code, and to have proper type checking of interfaces.
Neither he nor anyone else knew how to get all three, and he thought C++ must have the first two or it would not be C++. It bothered him for a couple of decades, especially since he had put function argument type checking into C and valued good interfaces. A renewed attempt came around 2003, when he and Dos Reis analyzed the problem and possible solutions, and a group at Indiana University began a project.
They thought a good system could be done in a couple of years and make it into C++11, but he now thinks many of the fundamental ideas were wrong. They were unconventional for C++, handled implicit conversions and mixed-type arithmetic poorly, and drew heavily from the functional community, which treats multiple types differently and lacks C++'s draconian performance requirements.
In the end, he says, it was too slow to compile, too hard to use, and did not run fast enough without optimizers beyond the state of the art. The approach treated a set of parameters as defining a set of operations reached through an indirection table, like virtual functions, and relied on optimizing that indirection away. They could not make that work, so they had to do something else.
同じチャンネルの記事
3:36:56EN
5:15:51EN
3:12:58EN
3:46:18EN
3:51:47EN
2:53:42EN