how to have alias this with an unaccessible member?
Simen Kjaeraas
simen.kjaras at gmail.com
Fri May 17 16:24:44 PDT 2013
On Sat, 18 May 2013 01:13:00 +0200, Timothee Cour
<thelastmammoth at gmail.com> wrote:
> How to have alias this with an unaccessible member (x below).
> Making the member private won't work as it'll disable all operations on
> said member.
>
> ----
> struct A(T){
> T x;
> //private T x would prevent alias this from doing anything useful
> alias x this;
> }
> void main(){
> auto a=A!int;
> a++;//should do a.x++;
> static assert(!__traits(compiles,a.x)); // I want this to hold
>
> }
> ----
The common way to do it is with a read-only property:
struct A(T) {
private T x;
@property
T get() {
return x;
}
alias get this;
}
void main(){
auto a = A!int;
a++; //should do a.x++;
static assert(!__traits(compiles, a.x)); // This now holds!
static assert(__traits(compiles, a.get)); // As does this, which may or
may not be palatable. :(
}
This has been discussed numerous times before, but I believe the current
behavior is here to stay.
--
Simen
More information about the Digitalmars-d-learn
mailing list