简体   繁体   English

C ++如何知道容器类有push_back函数?

[英]How does C++ know the container class has a push_back function?

When I look at std::back_insert_iterator http://en.cppreference.com/w/cpp/iterator/back_insert_iterator 当我看到std::back_insert_iterator http://en.cppreference.com/w/cpp/iterator/back_insert_iterator

It says the container's push_back method will be called. 它说将调用容器的push_back方法。 How does it know if the container has a method of push_back? 如何知道容器是否有push_back方法? Does it require a class that extends any virtual class and where is it defined? 它是否需要一个扩展任何虚拟类的类以及它定义的位置?

It says the container's push_back method will be called. 它说将调用容器的push_back方法。 How does it know if the container has a method of push_back? 如何知道容器是否有push_back方法? Does it require a class that extends any virtual class and where is it defined? 它是否需要一个扩展任何虚拟类的类以及它定义的位置?

No, push_back() is usually not a virtual method. 不, push_back()通常不是虚方法。

The std::back_insert_iterator is a template, which is going to call push_back() on the passed object. std::back_insert_iterator是一个模板,它将在传递的对象上调用push_back() It the method is missing, you are going to get a compile error. 它缺少方法,你将得到一个编译错误。

It doesn't magically know this to be true. 它并不神奇地知道这是真的。 Instead, it fails to compile if it doesn't find such a function. 相反,如果找不到这样的函数,它就无法编译。

The operations (assignment in this case) which should call push_back are written using push_back and if the given container does not have a function with that name, the invocation of that function simply won't compile. 应该调用push_back的操作(在本例中为赋值)是使用push_back编写的,如果给定的容器没有具有该名称的函数,则该函数的调用将无法编译。

How does it know if the container has a method of push_back? 如何知道容器是否有push_back方法?

It doesn't. 它没有。 You'll get a compile error if you try to use it on a container that doesn't have push_back . 如果您尝试在没有push_back的容器上使用它,您将收到编译错误。

Does it require a class that extends any virtual class and where is it defined? 它是否需要一个扩展任何虚拟类的类以及它定义的位置?

No, it just requires a class that defines it. 不,它只需要一个定义它的类。 In general, templates don't use dynamic polymorphism via virtual functions; 通常,模板不通过虚函数使用动态多态性; they just require the code they contain (like container.push_back(thing) ) to be valid after argument substitution. 他们只需要它们包含的代码(如container.push_back(thing) )在参数替换后有效。

If you're talking about templates (the container sent as an template) then the compiler will try to expand the template and if it doesn't find the method, it will simply fail. 如果你在谈论模板(作为模板发送的容器),那么编译器将尝试扩展模板,如果它没有找到方法,它就会失败。 The templates are checked at compile time, so the compile will inform you that a container doesn't have the method you requested... 在编译时检查模板,因此编译将通知您容器没有您请求的方法...

It doesn't know, you should know it. 它不知道,你应该知道它。 You have to pass the container which has push_back to std::back_insert_iterator . 您必须将具有push_back的容器传递给std::back_insert_iterator Otherwise, it will cause to compile errors. 否则,将导致编译错误。

For example, if you use it for a set you will get this error: 例如,如果您将其用于set ,则会出现此错误:

'class std::set' has no member named 'push_back' 'class std :: set'没有名为'push_back'的成员

No, it does not need to extend a class. 不,它不需要扩展一个类。

std::back_insert_iterator is a template; std::back_insert_iterator是一个模板; and the compiler will find out if the type you're trying to use the template on has this method or not when he instantiates it. 并且编译器会在实例化时查明您尝试使用模板的类型是否具有此方法。

I think this type of requiring a certain interface via templates is called an Implicit Interface (although I can't find a definitive reference at this point) - in contrast to explicit interfaces exhibited by deriving from a certain (possibly abstract) class. 我认为这种通过模板需要某个接口的类型称为隐式接口 (虽然我在这一点上找不到确定的参考) - 与从某个(可能是抽象的)类派生的显式接口形成对比。

It is a templatised class. 这是一个模板化的课程。

It will generate the code at compile time. 它将在编译时生成代码。 In the compile time, it will look for push_back function in container. 在编译时,它将在容器中查找push_back函数。 If there is not push_back , it will not compile. 如果没有push_back ,它将无法编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM