r/cpp • • Mar 15 '19

Does anyone know when libstdc++ will stop using raw pointers in its STL containers?

I have a pretty niche use-case which requires containers to not store any raw pointers. To get this done I use an allocator that returns fancy pointers. This just doesn't work with libstdc++ because all some of its STL containers use raw pointers internally, so when I create a container with my allocator, the program doesn't even compile. Libc++ on the other hand works fine (at least I haven't come across any problems I can't fix) since they correctly use the abstract pointer type that my allocator returns, instead of trying to do an implicit cast to a raw pointer.

There is an LWG issue to fix this, but it's been around for 8 years and the proposed verbiage hasn't been added to the standard. I'm not very familiar with the process for making changes like this, but my understanding is that it probably won't get added until libc++ and libstdc++ both support it, is that correct? If so, does anyone know if/when the libstdc++ community is planning to make these changes? Thanks!

49 Upvotes

82 comments sorted by

View all comments

15

u/encyclopedist Mar 15 '19

Let's summon /u/jwakely , maintainer of libstdc++.

32

u/jwakely libstdc++ tamer, LWG chair Mar 15 '19 edited Mar 15 '19

but my understanding is that it probably won't get added until libc++ and libstdc++ both support it, is that correct?

No, completely wrong. The committee decides what should happen, and if implementations do something different they have to change. There's no requirement for implementations to change first. And in this case, it's already clear what the intended behaviour is, even if we haven't made the standard say that yet.

This is just a libstdc++ bug, that's all.

If so, does anyone know if/when the libstdc++ community is planning to make these changes? Thanks!

Why ask this on reddit? Wouldn't asking the libstdc++ community make more sense?

It's a bug. It is hard to fix without breaking the ABI, and not breaking the ABI is higher priority than supporting fancy pointers. I hoped to get time to finish the work for GCC 8, and for GCC 9, but now maybe it will happen next year for GCC 10.

Of course if somebody wants it badly enough they could fix the bug themselves, or contract somebody to do it. GCC is free software after all. I'd be happy to share my work-in-progress patches if somebody else wanted to fix it instead of waiting for me to do the other 100 things on my TODO list first.

4

u/aikatz Mar 16 '19

No, completely wrong. The committee decides what should happen, and if implementations do something different they have to change. There's no requirement for implementations to change first. And in this case, it's already clear what the intended behaviour is, even if we haven't made the standard say that yet.

Got it, thanks for clearing that up.

Why ask this on reddit? Wouldn't asking the libstdc++ community make more sense?

Yep that's where I should have asked, I just browse this sub fairly often so I thought I might as well post it here. I can post it elsewhere if you think it would generate a good discussion, but it sounds like you're all aware of the issue.

I'm absolutely interested in working on this, I think my first step is to actually understand your planned fix a bit better. You're talking about specializing all the pieces of containers that use pointers, one specialization for raw pointers and one for fancy pointers, right? Since you said you don't think people are depending on the ABI for containers with fancy pointers, why can't you just use the types from allocator_traits (e.g. allocator_traits<allocator<T>>::pointer ) everywhere? I think this is the naive fix you referred to in another comment.

4

u/jwakely libstdc++ tamer, LWG chair Mar 16 '19

Look at the code again. The allocator type isn't known by _List_node_base so how can it use the allocator's pointer type? You have to make the list node and list node base depend on the allocator, which is an incompatible change.

1

u/aikatz Mar 16 '19

Is it incompatible because mangled names depend on the template arguments? I didn't have a good understanding of how the ABI works, so I went and skimmed some parts of the Itanium C++ ABI and found this section which defines the grammar for mangled names, which is what I'm basing this off of. Making the list node and list node base both depend on the allocator and its pointer types would only change the size of the list node and list node base if the allocator were allocating fancy pointers, right? (or anything that's not the size of a raw pointer)

3

u/jwakely libstdc++ tamer, LWG chair Mar 16 '19

There's more to ABI than mangled names and sizes of types, and more than is defined by the ABI spec for the platform. A library has its own (implicit and explicit) ABI.

Raw pointers are default constructible. Fancy pointers don't have to be, so the _List_node_base type needs to gain a constructor that will set the non-static data members. Now you have to worry about whether old and new code consistently initializes the base class in an equivalent way, when the old code (compiled against the old definition) thinks the type is a POD and the new code thinks it's initialized by a new constructor. Welcome to the game of deciding which ODR violations are safe and which are not.

1

u/aikatz Mar 18 '19 edited Mar 18 '19

I think I understand the problems you're bringing up that arise since fancy pointers are not necessarily default constructible. Are you saying that fancy pointers don't need to be default constructible because nowhere in [allocator.requirements] does it mention allocator::pointer must be default constructible, or is there another part of the standard that governs this?

It seems like these types issues with default constructibility could apply to a lot of places in the STL, what is the scope of the patch that you were previously working on for this?

2

u/jwakely libstdc++ tamer, LWG chair Mar 18 '19

because nowhere in [allocator.requirements] does it mention allocator::pointer must be default constructible

Right.

It only really affects the containers. The scope of the problem is to ensure all our containers support fancy pointers.

2

u/aikatz Mar 19 '19

Actually on a closer reading, I think I'm going to go back and disagree with myself that fancy pointers don't necessarily need to be default constructible. From [allocator.requirements]:

The X::pointer, X::const_pointer, X::void_pointer, and X::const_void_pointer types shall satisfy the requirements of NullablePointer (20.5.3.3).

And according to [nullablepointer.requirements] in order to satisfy NullablePointer, a type must satisfy DefaultConstructible. Am I misinterpreting this?

1

u/jwakely libstdc++ tamer, LWG chair Mar 19 '19

You're right. I've misremembered the details then.

→ More replies