Is there a pure virtual function in the C++ Standard Library?
Asked Answered
Y

2

34

In Nicola Gigante's lecture in 2015, he mentions (at the beginning) that there are no pure virtual functions in the Standard Library (or he's not aware of any). I believe that Alex Stepanov was against this language feature but since the initial STL design, have any pure virtuals creeped into the Standard library?

FWIW (and correct me if I'm wrong) the deleters in unique pointers ultimately use virtual dispatching in most implementations but these are not pure virtuals.

Yardman answered 26/1, 2016 at 23:38 Comment(14)
Do you mean the entire Standard Library or just the STL (iterators, algorithms and containers) part?Thithia
I'm not aware of any pure virtual functions in the standard library. The default deleters for unique_ptr are very non-virtual, so unsafe if you cast up to non-polymorphic base class. shared_ptr on the other hand, keeps a type-erased deleter function with the original pointer, so is safe that way.Lighting
Any class having a pure virtual function wouldn't be instantiable. I'm unaware of any standard classes that are only meant to be used as base classes and not directly usable themselves.Cranston
I just watched this yesterday! I should've come here and asked. ;)Domitian
I have not listened to the lecture but it appears to be about generic programming and the STL. I suspect then that the point is that, in C++ generic programming as implemented in the STL is completely orthogonal to what might be considered typical methods in Object Oriented Programming.Thithia
Exactly the question I had on my mind after listening to the talk ;)Untutored
Do you want it to be specified as being pure virtual, or would a particular std library implementation using pure virtual methods qualify?Claw
STL != Standard LibraryCoarsen
@Coarsen I think I'm making a very clear distiction and only use the term "Standard Library". STL is correctly used to refer to the library designed by Stepanov and created in SGIYardman
@LorahAttkins: You imply that the STL preceded the Standard Library. That's not exactly the case: there was a significant draft Standard Library back in 1995, when the STL was merged into the Standard Library.Bogusz
@Bogusz No, I'm not. I'm saying that Stepanov, who designed the STL, was not a fan of virtuals, so that part of the Standard libray was likely "virtual-free"; but since then does the stdlib (as a whole - even the parts that evolved from the STL and the legacy C libraries - that are even more difficult to add new styles) have any virtual calls?Yardman
@Bogusz the "significant draft Standard Library back in 1995" was mostly the C standard libraryYardman
@LorahAttkins: No it wasn't. Just to name a few parts that are neither derived from C nor part of the STL : The whole of <iostream>, std::string, std::complex, exceptions. Now std::string and std::complex are generally too time-critical to use virtual functions, nor is there a need to - polymorphism simply isn't needed for straightforward values. But <iostream> and exceptions do use virtual functions.Bogusz
@Bogusz Basic building blocks simply have no use of customization based on virtual function overriding. I suspect that people (typically Java programmers shy with final) that make many functions virtual in fundamental concrete classes (like containers) simply wouldn't be able to specify what it means, abstractly, to override these functions, and what the overrider would be allowed to do.Tune
L
59

[syserr.errcat.overview] has std::error_category

class error_category {
  virtual const char* name() const noexcept = 0;
  virtual string message(int ev) const = 0;
};

There are no others in C++14.

Lobar answered 27/1, 2016 at 0:1 Comment(5)
How are you certain there are no others? Just curious. (P.S. +1)Olander
@Olander Searched the text of the standard, from chapter 17 onward, for = 0. Eyeballed every hit (there are not that many). Good reason to procrastinate on doing actual work.Lobar
Note that a typical implementation of std::function will use pure virtual functions as an implementation detail (or, reproduce an equivalent with C-style OO). The same may be true of future and other run time concept/type erasure types.Claw
I guess one question here is, would it be against the standard for these functions not to be purely virtual? (Would any program fail to behave correctly if that was the case?)Northern
@Mehrdad I don't see how it could cause a conforming program to stop working. However, it would allow a non-conforming program to be accepted - a program that has a class derived from std::error_category which fails to override one of those methods. This in itself could be argued to be non-conforming on the part of the implementation - the fact that an invalid program is accepted with no diagnostic.Lobar
D
10

C++17 adds std::pmr::memory_resource in [mem.res.class] to the one in C++14, with the following private pure virtual functions:

class memory_resource {
    virtual void* do_allocate(size_t bytes, size_t alignment) = 0;
    virtual void do_deallocate(void* p, size_t bytes, size_t alignment) = 0;
    virtual bool do_is_equal(const memory_resource& other) const noexcept = 0;
};

And yes, private virtual functions can be overridden.

Dysphonia answered 26/12, 2018 at 7:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.