C++ Logo

std-proposals

Advanced search

Re: More general version of const_iterator

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Tue, 3 Mar 2020 19:47:45 +0100
Thiago Macieira wrote:
>
> On Monday, 2 March 2020 13:25:45 PST Marcin Jaczewski via Std-Proposals wrote:
> > In my recent work I stumbled on problem that I need have two types
> > that have same members but different `const`-nes of them:
> >
> > ```
> > class C
> > {
> > X* a;
> > X* b;
> > Y* c;
> > };
> >
> > class C_const
> > {
> > const X* a;
> > const X* b;
> > const Y* c;
> > };
> > ```
>
> Note that this is pointer-to-const, not a const pointer.
>

Yup, for "const pointer" we have simply `const` for that :)
But not so luxury for `const_iterator` :>

> Now, what do you suppose your const_inner proposal should do to:
>

Overall idea was for view/observer classes that do not own this resources.
And only used as const/not-const access to inner data of other class.

> struct C2
> {
> std::unique_ptr<X> a;
> };
>
> struct C3
> {
> std::vector<X> a;
> };
>

This both classes are owning, this helper alias could generate error
or leave type as is.


> struct C4
> {
> std::vector<X>::iterator a;
> };
>

"const" version would look like:

```
struct C4_const
{
    std::vector<X>::const_iterator a;
};
```
And I do not suggest functionality that can convert `C4` to `C4_const`
but switch type for `a` using helper alias.
Something like synthesizing `C4_const` would require language change
(probably impossible to make it correctly, too many corner cases) or
use static reflection.
With reflection this is doable but it still would befenci from
functionality I suggest.
This would be convention that allow to decide what operation do exactly,
because some times types can can be owners or views depending on context (`T*`).



> struct C5
> {
> std::unordered_map<X, Y> a;
> };
>
> struct C6
> {
> std::aligned_union<8, X, Y>::type a;
> }
>
> --
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
> Software Architect - Intel System Software Products

Received on 2020-03-03 12:50:37