While reading about C++17 library features, I found public methods that simply call private pure virtual methods. For example:
std::pmr::memory_resource::allocate
void* allocate( std::size_t bytes, std::size_t alignment = alignof(std::max_align_t) );
Allocates storage with a size of at least bytes bytes, aligned to the specified alignment.
Equivalent to
return do_allocate(bytes, alignment);
std::pmr::memory_resource::do_allocate
virtual void* do_allocate( std::size_t bytes, std::size_t alignment ) = 0;
and
std::basic_streambuf<CharT,Traits>::pubsync, std::basic_streambuf<CharT,Traits>::sync
(1)
int pubsync();
protected:
(2)
virtual int sync();
- Calls
sync()
of the most derived class
why such pattern is used?
I read What is the point of a private pure virtual function? and some articles about NVI idiom, but nothing explains the above cases that a method does nothing but calls private method and has no overloaded methods.