C++ Logo

std-proposals

Advanced search

[std-proposals] decltype can access private and protected

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Fri, 25 Mar 2022 17:46:49 +0000
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: To get the type of
protected/private members, for example:

class Dog {
private:
        int calibration[3u][2u][7u];
};

int main(void)
{
    Dog doggy;

    decltype(doggy.calibration) my_array;
}

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.

Received on 2022-03-25 17:46:51