C++ Logo

std-proposals

Advanced search

[std-proposals] Trivial Relocatability - A better place for these specifiers

From: Wojciech Kwiliński <W.Kwilinski_at_[hidden]>
Date: Mon, 10 Feb 2025 11:03:07 +0100
Hello!

I want to express my concerns regarding the syntax proposed in "Trivial
Relocatability for C++" (P2786R11).

The authors of P2786 use the class property specifiers
"memberwise_trivially_relocatable"
and "memberwise_replacable" to signify that a class is trivially
relocatable, like so:

     class MyClass
         memberwise_trivially_relocatable
         memberwise_replacable
     {
         // You still need to define these!
         MyClass(MyClass&&) {
             /* code */
         }
         MyClass& operator=(MyClass&&) {
             /* code */
         }
     };

My primary issue with this syntax is how 'out of place' the specifiers are.
They are related to the move special member functions, yet are nowhere
near them.
In fact, I would say that the specifiers are in the last place one would
look.

This is a rather large issue in terms of ease-of-use, since most classes
are trivially relocatable.

Consider the classic rule-of-five. While writing a class that follows
this rule,
we define the constructor, the copy constructor, the copy assignment,
the move constructor,
the move assignment, and the destructor. And then, we need to go back to
add the specifiers
(which, again, will be the case for most classes!).

I propose the following replacement:

     class MyClass {
         MyClass(MyClass&&) [[memberwise_trivial]] {
             /* code */
         }
         MyClass& operator=(MyClass&&) [[memberwise_trivial]] {
             /* code */
         }
     };

The main point of this change is to move the specifiers into a location
more suited
to their meaning. We also won't need to differentiate between 'trivial
move construct'
and 'trivial move assign'. That's up to where you place these attributes.

The reason why I propose these as attributes, and not as specifiers, is
that a
function definition with a requires-clause cannot have a
'virt-specifier-seq'.

I'm also not a fan of the rather lengthy 'memberwise_trivial', and would
prefer
just 'trivial', but I can accept that this might be a bit vague.

What do you think of this change?

Received on 2025-02-10 10:03:10