C++ Logo

std-proposals

Advanced search

Re: [std-proposals] consteval int relocatability

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Mon, 10 Nov 2025 09:41:37 +0000
On Mon, Nov 10, 2025 at 9:01 AM Frederick Virchanza Gotham wrote:
>
> I wonder had anyone considered altering the specification of 'qsort'
> so that it internally invokes 'restart_lifetime'? This would mean we
> have access to the old address and can therefore pass two addresses to
> 'restart_lifetime'. Or would it be too funky to have a C library
> function invoking a C++ library function like that? Well actually the
> C library function would be invoking a compiler builtin:
> __restart_lifetime.


The following excerpt from the Standard:

26.13 [alg.c.library] C library algorithms

void qsort(void* base, size_t nmemb, size_t size, c-compare-pred* compar);
void qsort(void* base, size_t nmemb, size_t size, compare-pred* compar);

2 Preconditions: For qsort, the objects in the array pointed to by base are
   of trivially copyable type.

3 Effects: These functions have the semantics specified in the C standard
   library. [ ... Throws: any exception thrown by compar ... ]


could be changed as follows:


[alg.c.library] C library algorithms

void qsort(void* base, size_t nmemb, size_t size, c-compare-pred* compar);
void qsort(void* base, size_t nmemb, size_t size, compare-pred* compar);

2 Preconditions: For qsort, base points to the first element of an array of
     nmemb contiguous objects of some type T ([basic.types]). T is a Trivially
    Relocatable type (see [basic.life]) and the argument compar imposes a
    strict weak ordering on those objects.

3 Effects: These functions have the semantics specified in the C
standard library.
    Additionally, if the array elements have type T and an element whose stored
    value is located at address p_old is moved by the function to
address p_new within the
    array, then the lifetime of the object at p_old ends and the
lifetime of a T object at p_new
    begins as if by
            std::restart_lifetime( static_cast<T*>(p_old),
                                    static_cast<T*>(p_new));

([basic.life]).

[ Note 1: As a consequence, within the comparison function, a pointer
argument received
  from qsort that points into the array designates a T object whose
lifetime has begun and
  can be accessed through a T* or T& obtained via
std::restart_lifetime. — end note ]

Received on 2025-11-10 09:41:52