C++ Logo

std-proposals

Advanced search

Re: Proposal of constpub (DRAFT 1)

From: Михаил Найденов <mihailnajdenov_at_[hidden]>
Date: Tue, 14 Jan 2020 10:41:21 +0200
On Mon, Jan 13, 2020 at 7:15 PM Matthew Woehlke via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On 21/11/2019 05.33, Andrew Tomazos via Std-Proposals wrote:
> > Please find attached a 2-page draft proposal entitled:
> >
> > Proposal of constpub
> >
> > It seems a little thing but easy to implement and I think it may have
> broad
> > appeal.
> >
> > Anyway, initial thoughts appreciated.
> > -Andrew.
>
> I would rather see:
>
> class foo
> {
> public:
> using x = std::as_const(m_x);
> protected:
> int m_x;
> }
>
> No new keywords, leverages the idea of "universal aliases", has more
> possible uses...
>

I agree, that it is better to have new type of alias/reference then to have
a niche keyword, and that the price to have a diff name it is worth it,
 because, if we are to add an observer function, we have to use a different
syntax anyway.

Few months back I played with that idea and figured out, a completely new
reference can help us "restore" the "aggregate-ness" of class, lost by
adding a private member

Consider

class C
{
public:
  const auto x -> m_x;

protected:
  int m_x;
}

C c{12}; //< works!
auto [x1] = c; //< works!
auto x2 = std::get<0>(c); //< works!

Also, we can reuse a struct, and rename files

struct Payload : std::pair<int, std::string>
{
  auto key -> first;
  auto value -> second;
};

The most interesting part is, that we have all the tools to implement this,
as designators *can simply be regular members, declared as*
[no_unique_address]*.*

assert(std::addressof(c.x) == std::addressof(c.m_x));

Once we have a new members we can redefine operator& for them and get a
bonus feature:

template<Func F>
void work(F f) { ... f(c); }

C c{12};

work(&C::x); //< creates function object with signature int(const C&)



> --
> Matthew
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2020-01-14 02:44:07