C++ Logo

std-proposals

Advanced search

Re: [std-proposals] decltype can access private and protected

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Fri, 25 Mar 2022 19:27:47 +0100
On 25/03/2022 18:46, Frederick Virchanza Gotham via Std-Proposals wrote:
>
> Normally the behaviour is undefined when you dereference a nullptr, but it's allowed with decltype, for example the following is valid code:
>
> decltype( static_cast<std::string*>(nullptr)->compare("Hello") ) my_var;
>
> ++my_var;
>
> I think decltype should be given another privilege:

Random note: the code above has not been idiomatic C++ for at least the
last 11 years; you're supposed to use std::declval.


> This past week I was writing code for an Arduino microcontroller to save
> POD's to the onboard non-volatile Flash, and I had to edit a class to
> change a member from protected to public just so that I could use
> decltype on it. This shouldn't be necessary -- just allow decltype to
> get the type of anything irrespective of accessibility.

A proposal to allow bypassing one of the most fundamental means of
achieving encapsulation of C++ should have much stronger motivations
than "it's annoying once to me in this corner case".

If you don't have the means of using that member anyhow because it's
private, why do you care about its type?

If you have a way to access that member via an indirection (e.g. a
getter), why not using decltype() on *that* instead?

If the class in question is meant to be privately accessed by your code,
why not befriending such code?

If the type of the member is important to clients of the class, why
doesn't the class itself expose it as a public type alias (e.g.
value_type in std::complex)?

And finally, what makes decltype() special in regards bypassing
accessibility checks, and not, say, sizeof()?


My 2 c,
-- 
Giuseppe D'Angelo | giuseppe.dangelo_at_[hidden] | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

Received on 2022-03-25 18:27:56