Source

ConstCPP / ConstCPPTests / examples.cpp

extern "C" {
    int printf( char *, ...);
}


int f() {
    int a = 0;         // <- const int a = 0;
    mutable int b = 1; // <-       int b = 1;
    b = 2;
    return a + b;
}

struct Normal {
    mutable int a, b;
    
    Normal() : a(0), b(0) {}
    ~Normal() {}
};

int f2() {
    mutable Normal n,n2;
    n.a = 1;
    n.b = 1;
    n2  = n;
    return n2.a + n2.b;
}

struct PartMutable {
    int a;
    mutable int b;
    PartMutable() : a(0), b(0) {}
    PartMutable(int a, int b) : a(a), b(b) {}
    ~PartMutable() {}
};

int f3() {
    mutable PartMutable n;
    n.b = 1;
    return n.a + n.b;
}

struct NotMutable {
    int a, b;
    NotMutable() : a(0), b(0) {}
    NotMutable(int a, int b) : a(a), b(b) {}
    ~NotMutable() {}
    
    int getA() {      printf("Not mutable\n"); return a + 1; }
    int getA() mutable {  printf("Mutable\n"); return a - 1; }
};

int f4() {
    mutable NotMutable n;
    NotMutable n2;
     
    return n2.getA() + n.getA();
}

int f5(int* p) { return *p; }
int f5(mutable int* p) { *p = 1; return 0; }

int f5() {
    mutable int a = 10;
    int b = 15;
    int r1 = f5(&a);
    int r2 = f5(&b);
    if (r1 == 0 && a == 1 && r2 == 15)
        return 1;
    return 0;
}

int main(int argc, char** argv) {
    if (!(f() == 2 )) return 1;
    if (!(f2() == 2 )) return 1;
    if (!(f3() == 1 )) return 1;
    if (!(f4() == 0 )) return 1;
    if (!(f5() == 1 )) return 1;
    
    
}