C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Public inheritance with banned conversion to base

From: Breno Guimarães <brenorg_at_[hidden]>
Date: Fri, 24 Jan 2025 19:50:46 -0300
That's it:

struct FooVec : public std::vector<Foo> {
   using std::vector<Foo>::vector; // gotta expose the constructor for some
reason. I guess that's the syntax
};

Now you can do:
class FooVec;

Em sex., 24 de jan. de 2025, 19:46, Arthur O'Dwyer <
arthur.j.odwyer_at_[hidden]> escreveu:

> On Fri, Jan 24, 2025 at 5:20 PM Breno Guimarães <brenorg_at_[hidden]> wrote:
>
>> I also inherit from some containers to allow them to be forward declared
>> and reduce the amount of template instantiation. I don't add any new
>> functions or members so everything works well.
>> I don't do it for all types but just some vocabulary types.
>>
>> Not sure if anyone mentioned that usecase yet (or if there is a
>> categorily better option).
>>
> Could you show an example of what you mean? I can't picture it.
>
> I did see that some uses in LLVM seem like people are writing
> struct MyVec : std::vector<int> {};
> simply as a kind of "mangling firewall," where the "natural" thing to
> write (in the absence of any external constraints) would have been simply:
> using MyVec = std::vector<int>;
> In the latter case the mangled symbol names would have contained
> "std::__1::vector<int, std::__1::allocator<int>>"; in the former case
> they'll simply contain "MyVec", which is (1) shorter, (2) easier to read in
> the debugger, and (3) more stable in case this is a long-lived
> shared-library ABI boundary.
>
> –Arthur
>

Received on 2025-01-24 22:50:58