OOP든 STL이든 잘 돌면 장땡!
http://stackoverflow.com/questions/1039853/why-is-the-c-stl-is-so-heavily-based-on-templates-and-not-on-interfaces 의 답변 간단 번역
간단히 말하자면, C++ 은 변해왔거든. 70년대 Stroustrup 이 OOP 기능을 넣어 C를 향상시키려고 했어. 근데 시간은 흐르고, 정작 C++ 표준화가 되었을 1998년에는 언어 자체가 더이상 OOP 언어가 아니게 된거지. 다중-패러다임 언어가 되었네. 물론 여전히 OOP를 지원하지만, 튜링-완전한 템플릿 언어가 가세한거야. 이 변화가 컴파일타임 메타프로그래밍을 가능케 하고, 사람들은 generic programming를 발견하지. 그러니까 갑자기 OOP가 그리 중요한 문제가 아니게 된거야. 이런 새 기능을 가지고 간단하고, 짧고, 더 효과적인 코드를 쓸수 있게 됐어
OOP는 만병통치약이 아니야. 깜찍한 생각이지 그리고 그게 처음 만들어진 70년대에는 절차지향 언어에서 꽤 진일보한것었어. 그런데 솔직히 모든면에서 좋아진건 아니란말야. 많은 경우, 엉성하고 줄만길어져 그리고 진정한 재사용가능 코드나 모듈화를 이루지 못했어.
그래서 C++ 커뮤니티가 최근 generic programming에 더 관심을 가지게 된거고 왜 모두들 functional programming 이 똑똑하다는 걸 깨닫기 시작했지. OOP 그 자체로는 별로 아름답지 못하거든.
가상적 OOP화된 STL의 의존 그래프를 그려봐. 얼마나 많은 클래스가 서로 알고 연결될것 같아? 꽤 많은 의존관계가 있겠지. 그 안에서 단지 vector 헤더만 include하는게 가능할까, <iterator>나 <iostream>을 같이 넣지 않고 말야. STL은 이걸 쉽게 만들었어. vector는 각 요소를 정의하는 iterator타잎을 알고 있고 그게 다야. STL 알고리즘은 아무 것도 모르지. 심지어 iterator 헤더도 없이, iterator 를 인자로 받아 들여. 그럼 둘중 어떤게 더 모듈화 된거지?
STL이 Java처럼 OOP룰을 잘 안따를 수 있어. 하지만 OOP가 지향하는 목적을 이룰수 있지않아? 재사용가능성, 낮은 결합도, 모듈화, 자료은닉 같은 것 말야.
원문:
The short answer is "because C++ has moved on". Yes, back in the late 70's, Stroustrup intended to create an upgraded C with OOP capabilities, but that is a long time ago. By the time the language was standardized in 1998, it was no longer an OOP language. It was a multi-paradigm language. It certainly had some support for OOP code, but it also had a turing-complete template language overlaid, it allowed compile-time metaprogramming, and people had discovered generic programming. Suddenly, OOP just didn't seem all that important. Not when we can write simpler, more concise and more efficient code by using techniques available through templates and generic programming.
OOP is not the holy grail. It's a cute idea, and it was quite an improvement over procedural languages back in the 70's when it was invented. But it's honestly not all it's cracked up to be. In many cases it is clumsy and verbose and it doesn't really promote reusable code or modularity.
That is why the C++ community is today far more interested in generic programming, and whyeveryone are finally starting to realize that functional programming is quite clever as well. OOP on its own just isn't a pretty sight.
Try drawing a dependency graph of a hypothetical "OOP-ified" STL. How many classes would have to know about each others? There would be a lot of dependencies. Would you be able to include just thevector
header, without also getting iterator
or even iostream
pulled in? The STL makes this easy. A vector knows about the iterator type it defines, and that's all. The STL algorithms know nothing. They don't even need to include an iterator header, even though they all accept iterators as parameters. Which is more modular then?
The STL may not follow the rules of OOP as Java defines it, but doesn't it achieve the goals of OOP? Doesn't it achieve reusability, low coupling, modularity and encapsulation?
And doesn't it achieve these goals better than an OOP-ified version would?
As for why the STL was adopted into the language, several thnigs happened that led to the STL.
First, templates were added to C++. They were added for much the same reason that generics were added to .NET. It seemed a good idea to be able to write stuff like "containers of a type T" without throwing away type safety. Of course, the implementation they settled on was quite a lot more complex and powerful.
Then people discovered that the template mechanism they had added was even more powerful than expected. And someone started experimenting with using templates to write a more generic library. One inspired by functional programming, and one which used all the new capabilities of C++.
He presented it to the C++ language committee, who took quite a while to grow used to it because it looked so strange and different, but ultimately realized that it worked better than the traditional OOP equivalents they'd have to include otherwise. So they made a few adjustments to it, and adopted it into the standard library.
It wasn't an ideological choice, it wasn't a political choice of "do we want to be OOP or not", but a very pragmatic one. They evaluated the library, and saw that it worked very well.
In any case, both of the reasons you mention for favoring the STL are absolutely essential.
The C++ standard library has to be efficient. If it is less efficient than, say, the equivalent hand-rolled C code, then people would not use it. That would lower productivity, increase the likelihood of bugs, and overall just be a bad idea.
And the STL has to work with primitive types, because primitive types are all you have in C, and they're a major part of both languages. If the STL did not work with native arrays, it would be useless.
Your question has a strong assumption that OOP is "best". I'm curious to hear why. You ask why they "abandoned classical OOP". I'm wondering why they should have stuck with it. Which advantages would it have had?