C++ Logo

std-proposals

Advanced search

Re: [std-proposals] about incrementing a pointer of a type with pure virtual function

From: Thiago Macieira <thiago_at_[hidden]>
Date: Sun, 22 Jan 2023 00:32:19 -0800
On Saturday, 21 January 2023 06:20:25 PST Edward Catmur via Std-Proposals
wrote:
> Right. The existence of that unary array is somewhat questionable, since it
> would not be possible to create such an array within the program. That
> aside, you are correct which means that the UB occurs in the next
> iteration, when the past the end iterator is indirected.

Which points to a valid use-case of sizeof in an abstract class and pointer
math with it.

template <typename It, typename L> void apply(It begin, It end, L lambda)
{
   for ( ; begin <= end; ++begin)
        lambda(*begin);
}

void f(I *ptr)
{
    apply(ptr, ptr+1, [](auto p) { p->something(); });
}

I'm not saying this is good code, but it's perfectly valid. I know I've used
the "any object is an array of 1" technique.

https://gitlab.com/thiagomacieira/qtbase/-/blob/master/src/corelib/tools/
qgenericarray.h#L310-313
(this code is not in production, it was a prototype)

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-01-22 08:32:22