我的意思是,除了它的名字标准模板库(后来演变成c++标准库)。

c++最初将面向对象的概念引入到C中,也就是说:您可以根据特定实体的类和类层次结构来判断它能做什么和不能做什么(不管它是如何做的)。由于多重继承的复杂性,一些能力的组合更难以用这种方式描述,而且c++以一种有点笨拙的方式支持仅接口继承(与java等相比),但它确实存在(并且可以改进)。

然后模板开始发挥作用,还有STL。STL似乎采用了经典的OOP概念,并将它们冲入下水道,取而代之的是使用模板。

当模板用于泛化类型时,类型本身与模板的操作无关(例如容器),这两种情况之间应该有区别。vector<int>非常有意义。

然而,在许多其他情况下(迭代器和算法),模板化类型应该遵循一个“概念”(Input Iterator, Forward Iterator,等等),其中概念的实际细节完全由模板函数/类的实现定义,而不是由模板使用的类型的类定义,这在某种程度上是对OOP的反使用。

例如,你可以告诉函数:

void MyFunc(ForwardIterator<...> *I);

更新:由于在最初的问题中不清楚,ForwardIterator本身可以被模板化以允许任何ForwardIterator类型。相反,将ForwardIterator作为一个概念。

只有通过查看它的定义才能期望Forward Iterator,在这里你需要查看以下方面的实现或文档:

template <typename Type> void MyFunc(Type *I);

我可以提出两个主张来支持使用模板:通过为每个使用的类型重新编译模板,而不是使用动态分派(主要通过虚表),可以使编译后的代码更加高效。2. 事实上,模板可以与原生类型一起使用。

然而,我正在寻找一个更深刻的原因,放弃经典的面向对象的支持模板的STL?


当前回答

STL最初的目的是提供一个涵盖最常用算法的大型库,其目标是一致的行为和性能。模板是使实现和目标可行的关键因素。

只是为了提供另一个参考:

1995年3月,《DDJ》的艾尔·史蒂文斯采访亚历克斯·斯捷潘诺夫:

http://www.sgi.com/tech/stl/drdobbs-interview.html

Stepanov解释了他的工作经验和对大型算法库的选择,最终演变为STL。

Tell us something about your long-term interest in generic programming .....Then I was offered a job at Bell Laboratories working in the C++ group on C++ libraries. They asked me whether I could do it in C++. Of course, I didn't know C++ and, of course, I said I could. But I couldn't do it in C++, because in 1987 C++ didn't have templates, which are essential for enabling this style of programming. Inheritance was the only mechanism to obtain genericity and it was not sufficient. Even now C++ inheritance is not of much use for generic programming. Let's discuss why. Many people have attempted to use inheritance to implement data structures and container classes. As we know now, there were few if any successful attempts. C++ inheritance, and the programming style associated with it are dramatically limited. It is impossible to implement a design which includes as trivial a thing as equality using it. If you start with a base class X at the root of your hierarchy and define a virtual equality operator on this class which takes an argument of the type X, then derive class Y from class X. What is the interface of the equality? It has equality which compares Y with X. Using animals as an example (OO people love animals), define mammal and derive giraffe from mammal. Then define a member function mate, where animal mates with animal and returns an animal. Then you derive giraffe from animal and, of course, it has a function mate where giraffe mates with animal and returns an animal. It's definitely not what you want. While mating may not be very important for C++ programmers, equality is. I do not know a single algorithm where equality of some kind is not used.

其他回答

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.

面向对象编程不是圣杯。这是一个可爱的想法,它比70年代的过程语言有了很大的改进。但说实话,它并不像人们说的那么好。在许多情况下,它是笨拙和冗长的,并不能真正促进可重用代码或模块化。

这就是为什么c++社区今天对泛型编程更感兴趣,也是为什么每个人终于开始意识到函数式编程也是相当聪明的。OOP本身并不是一个美丽的景象。

Try drawing a dependency graph of a hypothetical "OOP-ified" STL. How many classes would have to know about each other? There would be a lot of dependencies. Would you be able to include just the vector 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?

STL可能不像Java定义的那样遵循面向对象的规则,但它不也达到了面向对象的目标吗?难道它没有实现重用性、低耦合性、模块化和封装性吗?

它能比oop化的版本更好地实现这些目标吗?

至于为什么STL被采用到语言中,发生了几件事导致了STL的出现。

首先,将模板添加到c++中。添加它们的原因与。net中添加泛型的原因大致相同。能够在不抛弃类型安全性的情况下编写“T类型容器”之类的东西似乎是个好主意。当然,他们确定的实现要复杂得多,功能也强大得多。

然后人们发现他们添加的模板机制比预期的更强大。有人开始尝试使用模板来编写更通用的库。它受到函数式编程的启发,并且使用了c++的所有新功能。

他把它提交给c++语言委员会,委员会花了很长时间才适应它,因为它看起来很奇怪,很不一样,但最终意识到它比传统的面向对象编程更好。所以他们对它做了一些调整,并将其纳入标准库。

这不是一个意识形态的选择,也不是一个“我们想不想成为面向对象”的政治选择,而是一个非常务实的选择。他们评估了这个图书馆,发现它运行得很好。

在任何情况下,你提到的支持STL的两个原因都是绝对必要的。

c++标准库必须是高效的。如果它的效率低于,比如说,同等的手工编写的C代码,那么人们就不会使用它。这将降低生产力,增加bug的可能性,并且总体来说是个坏主意。

STL必须和原始类型一起工作,因为原始类型是C语言中所有的,它们是两种语言的主要部分。如果STL不能与本机数组一起工作,那么它将是无用的。

你的问题强烈地假设面向对象编程是“最好的”。我很好奇为什么。你问为什么他们“放弃了经典的面向对象编程”。我在想他们为什么要坚持下去。它会有哪些优势?

在对STL作者Stepanov的采访中可以找到答案:

是的。STL不是面向对象的。我 认为面向对象是 几乎和人造的一样是骗局 情报。我还没见过 这段有趣的代码 来自这些面向对象的人。

这个问题有很多很好的答案。还应该提到模板支持开放设计。在面向对象编程语言的当前状态下,在处理此类问题时必须使用访问者模式,而真正的OOP应该支持多个动态绑定。参见c++的开放多方法,P. Pirkelbauer等。非常有趣的阅读。

模板的另一个有趣之处在于,它们也可以用于运行时多态性。例如

template<class Value,class T>
Value euler_fwd(size_t N,double t_0,double t_end,Value y_0,const T& func)
    {
    auto dt=(t_end-t_0)/N;
    for(size_t k=0;k<N;++k)
        {y_0+=func(t_0 + k*dt,y_0)*dt;}
    return y_0;
    }

注意,如果Value是某种类型的向量(不是std::vector,应该称为std::dynamic_array以避免混淆),则此函数也可以工作。

如果func很小,这个函数将从内联中获得很多。示例使用

auto result=euler_fwd(10000,0.0,1.0,1.0,[](double x,double y)
    {return y;});

在这种情况下,你应该知道确切的答案(2.718…),但是很容易构造一个没有初等解的简单ODE(提示:在y中使用多项式)。

现在,您在func中有一个大表达式,并且在许多地方使用ODE求解器,因此您的可执行文件到处都受到模板实例化的污染。怎么办呢?首先要注意的是,常规函数指针可以工作。然后,您希望添加curry,以便编写接口和显式实例化

class OdeFunction
    {
    public:
        virtual double operator()(double t,double y) const=0;
    };

template
double euler_fwd(size_t N,double t_0,double t_end,double y_0,const OdeFunction& func);

但是上面的实例化只适用于double,为什么不把接口写成模板呢:

template<class Value=double>
class OdeFunction
    {
    public:
        virtual Value operator()(double t,const Value& y) const=0;
    };

并专门化一些常见的值类型:

template double euler_fwd(size_t N,double t_0,double t_end,double y_0,const OdeFunction<double>& func);

template vec4_t<double> euler_fwd(size_t N,double t_0,double t_end,vec4_t<double> y_0,const OdeFunction< vec4_t<double> >& func); // (Native AVX vector with four components)

template vec8_t<float> euler_fwd(size_t N,double t_0,double t_end,vec8_t<float> y_0,const OdeFunction< vec8_t<float> >& func); // (Native AVX vector with 8 components)

template Vector<double> euler_fwd(size_t N,double t_0,double t_end,Vector<double> y_0,const OdeFunction< Vector<double> >& func); // (A N-dimensional real vector, *not* `std::vector`, see above)

如果函数首先是围绕接口设计的,那么您将被迫继承ABC。现在您有了这个选项,还有函数指针、lambda或任何其他函数对象。这里的关键是我们必须有operator()(),我们必须能够在它的返回类型上使用一些算术运算符。因此,在这种情况下,如果c++没有操作符重载,模板机制就会中断。

模板类型应该紧随其后 一个“概念”(输入迭代器,前进 迭代器,等等… 定义了概念的细节 完全由实现的 模板函数/类,而不是由 类所使用的类型的类 模板,这是一个 反对使用面向对象编程。

我认为您误解了模板对概念的预期用途。例如,前向迭代器是一个定义非常明确的概念。要找到使类成为前向迭代器必须有效的表达式,以及它们的语义(包括计算复杂性),可以查看标准或http://www.sgi.com/tech/stl/ForwardIterator.html(必须按照Input、Output和Trivial Iterator的链接查看所有内容)。

该文档是一个非常好的界面,“概念的实际细节”就定义在那里。它们不是由前向迭代器的实现定义的,也不是由使用前向迭代器的算法定义的。

STL和Java处理接口的方式有三个不同之处:

1) STL使用对象定义了有效的表达式,而Java定义了必须在对象上调用的方法。当然,有效表达式可能是一个方法(成员函数)调用,但这并不一定是必须的。

2) Java接口是运行时对象,而STL概念在运行时是不可见的,即使使用RTTI。

3)如果你不能使STL概念所需的有效表达式有效,当你实例化一些模板时,你会得到一个未指定的编译错误。如果您未能实现Java接口的必要方法,则会得到一个特定的编译错误。

第三部分是如果你喜欢某种(编译时)"duck typing":接口可以是隐式的。在Java中,接口是显式的:当且仅当一个类说它实现了Iterable时,它才是Iterable。编译器可以检查它的方法的签名是否都存在并且正确,但是语义仍然是隐式的(即它们要么被记录下来,要么没有,但只有更多的代码(单元测试)才能告诉你实现是否正确)。

In C++, like in Python, both semantics and syntax are implicit, although in C++ (and in Python if you get the strong-typing preprocessor) you do get some help from the compiler. If a programmer requires Java-like explicit declaration of interfaces by the implementing class, then the standard approach is to use type traits (and multiple inheritance can prevent this being too verbose). What's lacking, compared with Java, is a single template which I can instantiate with my type, and which will compile if and only if all the required expressions are valid for my type. This would tell me whether I've implemented all the required bits, "before I use it". That's a convenience, but it's not the core of OOP (and it still doesn't test semantics, and code to test semantics would naturally also test the validity of the expressions in question).

STL对你来说可能是OO,也可能不是,但它确实把接口和实现清晰地分开了。它确实缺乏Java在接口上进行反射的能力,并且它以不同的方式报告违反接口需求的情况。

你可以告诉函数。期望前向迭代器仅为 看它的定义,你需要看 实现或文档…

Personally I think that implicit types are a strength, when used appropriately. The algorithm says what it does with its template parameters, and the implementer makes sure those things work: it's exactly the common denominator of what "interfaces" should do. Furthermore with STL, you're unlikely to be using, say, std::copy based on finding its forward declaration in a header file. Programmers should be working out what a function takes based on its documentation, not just on the function signature. This is true in C++, Python, or Java. There are limitations on what can be achieved with typing in any language, and trying to use typing to do something it doesn't do (check semantics) would be an error.

That said, STL algorithms usually name their template parameters in a way which makes it clear what concept is required. However this is to provide useful extra information in the first line of the documentation, not to make forward declarations more informative. There are more things you need to know than can be encapsulated in the types of the parameters, so you have to read the docs. (For example in algorithms which take an input range and an output iterator, chances are the output iterator needs enough "space" for a certain number of outputs based on the size of the input range and maybe the values therein. Try strongly typing that.)

以下是Bjarne对显式声明接口的介绍:http://www.artima.com/cppsource/cpp0xP.html

In generics, an argument must be of a class derived from an interface (the C++ equivalent to interface is abstract class) specified in the definition of the generic. That means that all generic argument types must fit into a hierarchy. That imposes unnecessary constraints on designs requires unreasonable foresight on the part of developers. For example, if you write a generic and I define a class, people can't use my class as an argument to your generic unless I knew about the interface you specified and had derived my class from it. That's rigid.

从另一个角度来看,使用duck类型,您可以在不知道接口存在的情况下实现接口。或者有人可以故意编写一个接口,让你的类实现它,并咨询你的文档,看看他们不会要求任何你没有做过的事情。这是灵活的。

最基本的问题是

void MyFunc(ForwardIterator *I);

你如何安全地获取迭代器返回的东西的类型?对于模板,这是在编译时为您完成的。