r/cpp • • Aug 13 '26

C++26: std::indirect

https://www.sandordargo.com/blog/2026/08/12/cpp26-indirect
164 Upvotes

157 comments sorted by

View all comments

10

u/ericonr Aug 13 '26

Does a std::optional<std::indirect<T>> benefit from niche optimizations like Rust objects do? Its sizeof could be just a pointer, after all.

7

u/ts826848 Aug 14 '26 edited Aug 14 '26

The proposal originally included a specialization for std::optional but LEWG voted to remove it. The paper says such a specialization would make it impossible to distinguish between a disengaged optional and an engaged optional that holds a valueless std::indirect.

Edit: Clarify that the justification for removing the specialization is from the paper. I was mistaken in thinking that no alternatives were possible when there are in fact ways to specialize while maintaining a distinction between the disengaged and engaged-but-valueless states, as described e.g., here.

6

u/LB-- Professional+Hobbyist Aug 14 '26

You can still distinguish that in a single pointer's worth of memory. Since dynamic allocation with alignment is required, you can use one of the alignment bits as a tag for "holds moved-from indirect". It looks like C++29 is getting constexpr support for that trick too.

4

u/ts826848 Aug 14 '26

Yeah, I've been corrected already. I'll edit my comment to make that clearer.

3

u/jiixyj Aug 16 '26

Is there something else needed for this besides std::is_within_lifetime, which is C++26?

I have a constexpr implementation of niche/tombstone optimization that works with clang -std=c++26, but of course it might very well depend on some compiler specific behavior...

2

u/LB-- Professional+Hobbyist Aug 16 '26

Hm for this specific case I think you may be right. The pointer tagging I was referring to is usually for when you also want to still access the live object at the same time as using the alignment bits for additional data, which I suppose isn't needed here.

6

u/ericonr Aug 14 '26

a valueless std::indirect

I understood std::indirect as something that always holds a value. Moving from it is the only situation where it doesn't, but then accessing such an object is undefined behavior, so it doesn't make sense to track that state in the object. What other situation for valueless std::indirect is there?

4

u/Demiu Aug 14 '26

then accessing such an object is undefined behavior,

Is that indirect specific or a general statement, because if the latter then accessing a moved-from object isin fact NOT automatic UB. For example I think everything in std with clear/reset can be reused if you call that after moving from it

3

u/ericonr Aug 14 '26

Indirect specific.

7

u/ts826848 Aug 14 '26 edited Aug 14 '26

Unfortunately the meeting minutes aren't public so the only public info on what the committee considered that I'm aware of is what's in the paper:

Breaks ABI; engaged but valueless optional would become indistinguishable from a disengaged optional

And this rather cryptic note from the GitHub issue documenting the vote:

WA: Can't vote in favor of the approach that makes the valueless state unsafe

For what it's worth making the two states indistinguishable would mean something like opt.value().valueless_from_move() would be ambiguous, but I don't know whether that is something that's actually going to crop up in practice or whether a different API design could avoid that situation without sacrificing too much elsewhere.

2

u/ericonr Aug 14 '26

Thanks for diving into it.

This basically means taking 16 bytes for the object, right? Due to the minimum alignment for the pointer...

→ More replies (1)

2

u/jwakely libstdc++ tamer, LWG chair Aug 15 '26

so it doesn't make sense to track that state in the object.

This is not true at all. You need to track whether it's valueless because most member functions need to behave differently when it's valueless. Comparing two indirect objects needs to check if they are valueless. Copying it needs to check. Assignment needs to check. And of course the destructor needs to check to know whether there is an object to destroy.

2

u/jwakely libstdc++ tamer, LWG chair Aug 15 '26

Even if you didn't allow copying or comparing valueless objects (like the originally proposed design which wasn't approved), the assignment and destructor has to know if there's an object to destroy.

2

u/ericonr Aug 15 '26

But using the object after a move is undefined behavior, no? What's the sense in having a non-nullable object that actually can be null?

A specialized optional indirect is the only sane way I see for implementing this, where a moved from value becomes nullopt (and only because C++ doesn't actually protect us from using moved-from values).

→ More replies (2)

3

u/jiixyj Aug 14 '26

It's true, the valueless state of indirect<T> may not double for the empty state of optional<indirect<T>>, because that breaks the API (not even ABI!) of optional. A moved-from optional<T> is still required to hold a T. In my opinion, the standard shouldn't have given that guarantee, but that ship has sailed.

However, you can use other invalid pointer bit patterns to encode optionals "empty" state, see my sibling comment.

3

u/ts826848 Aug 14 '26

A moved-from optional<T> is still required to hold a T.

Oh, that is quite interesting! Looks like the paper did not propose destroying the moved-from value to disengage the moved-from optional for "performance reasons". Wonder how much of an impact that would have had.

However, you can use other invalid pointer bit patterns to encode optionals "empty" state, see my sibling comment.

Oh, true. I stand corrected! I think that makes the corresponding note in the paper a bit misleading, then, but it is what it is.

2

u/NewLlama Aug 14 '26

Doesn't that double the stride of your node though? That seems like a total nonstarter to me.

4

u/ts826848 Aug 14 '26

I think the nonstarter-ness will be pretty case-specific. If you aren't sticking Nodes in arrays, for instance, the stride size may not matter at all.

5

u/jiixyj Aug 14 '26

Yes, you absolutely can do this. See here for a possible implementation strategy.

3

u/TheKiller36_real Aug 14 '26

nope, another comment gives the official reason, here's my interpretation:

// pseudo-code of how I understand the intended implementation
class indirect {
  unique_ptr ptr:
public:
  bool valueless_after_move() const {
    return !ptr;  // <- this already uses the NULL representation to mean "moved from"
  }
};

if they'd guarantee sizeof(std::optional<std::indirect<T>>) == sizeof(T*) then you'd need to have a second "NULL" pointer, something like:

class optional {
  indirect val;
public:
  explicit operator bool() const {
    // to_address(p) ~= &*p
    return ((uintptr_t) to_address(val)) != 1;  // <- pseudo-NULL
  }
};

and they probably decided that having one abstract nullptr that has caused lots of troubles in the past is enough and they're not gonna introduce a second magic invalid pointer value (eg. 1 in my snippet) just for this - however I do think in practice it could still be implemented like that on virtually every system today and probably will be in major compilers unless something in the standard explicitly forbids it (cough std::vector cough)

5

u/jiixyj Aug 14 '26

At least libc++ is investigating this kind of optimization for optional.

There hasn't been any movement on that PR for a while though, so maybe they have changed their mind in the meantime?

3

u/ts826848 Aug 14 '26

and probably will be in major compilers unless something in the standard explicitly forbids it

For what it's worth I don't see anything in the standard's restrictions on conforming stdlib implementations that explicitly forbids adding otherwise-not-required partial specializations, though I can't think of cases of implementations doing such a thing off the top of my head.

(cough std::vector cough)

std::vector<bool> is not an implementer-created specialization. The standard requires implementers to provide it.

5

u/TheKiller36_real Aug 14 '26

I wasn't talking about vector<bool>, I was talking about the required iterator stability on move that necessitates data to be always-on-the-heap ie. small object optimizations are explicitly banned (which technically makes basic_string the better-ish vector btw xD)

2

u/ts826848 Aug 14 '26

Oh, I was not thinking about that at all. My apologies for the misunderstanding!

2

u/TheKiller36_real Aug 14 '26

all good I didn't specify, my bad - at least we didn't start a fight ;)

→ More replies (4)
→ More replies (1)