C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Paper on Interfaces (5 Pages)

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Fri, 24 Feb 2023 09:22:58 +0000
On Fri, Feb 24, 2023, Julien Villemure-Fr├ęchette wrote:
>
> class stack : private vector
> {
> public:
> using pop = vector::pop_back;
>
> // normal using declaration
> using vector::size;
> // this would be identical to
> // using size = vector::size;
> };


If the object in question was originally defined as a 'stack' then
this is fine. However if the original object was a 'vector', then it's
undefined behaviour to cast it to 'stack' and pretend that it was
originally defined as a 'stack'. Of course though, such a cast will
work fine on every C++ compiler except in cases where the compiler can
optimise for a lack of aliasing.

My proposal deals with the original object having been a 'vector' --
i.e. my proposal is useful in cases where you can't change the type of
the original object. My proposal isn't much use at all if you can
simply define the original object as a 'stack' instead of a 'vector'.

By the way, you mentioned something that I forgot to put in my paper.
The following snippet:

      interface lockable_bisem : std::binary_semaphore {
            void lock(void) noexcept(false) { acquire(); }
            void unlock(void) noexcept(false) { release(); }
      };

can also be written in shorthand as:

      interface lockable_bisem : std::binary_semaphore {
            lock = acquire;
            unlock = release;
      };

however you can only use this shorthand if the two methods have the
same signature (i.e. the same return value and the same count of
parameters and parameter types).

Received on 2023-02-24 09:23:11