0

How to create simple immutable int in C++? I have a function:

int NOD(const int &a, const int &b) {
    if (b == 0) {return a;}
    const int remainder = a % b;
    return NOD(b, remainder);
}

which supposed to take two ints and returns greatest common divisor. But I didn't find a way to make a and b immutable, hence they can be altered within function, for example if we add a line in function:

*(int *)&a = 555;

it breaks everything. How to make immutable variables in C++?

USER
  • 83
  • 1
  • 11
  • 6
    yes, if you cast away all your protections, you don't get any protection. At some point, it's just memory and memory can be written to. – xaxxon Sep 27 '16 at 07:02
  • @xaxxon is there a way to say: 1. This memory is not changeable and 2. This reference to this memory is not changeable? – USER Sep 27 '16 at 07:04
  • There may be some sort of operating system option to do something like that, but it isn't a part of the C++ standard. References are never changeable. – xaxxon Sep 27 '16 at 07:06
  • 4
    Honestly, you could liken this to Patient: "Doctor, it hurts when I do this..." - Doctor: "Then don't do that." Seriously though, in this case, why not just pass `const int a, const int b`. It's not like those `int` values are expensive to copy on the call. – WhozCraig Sep 27 '16 at 07:07
  • 2
    May I ask, why the arguments of NOD are passed by references (and not by values)? – user3161880 Sep 27 '16 at 07:16
  • It is possible to implement GCD (Euclid's algorithm) using recursive templates with integer template arguments. Everything is essentially done at compile time. – Spencer Sep 28 '16 at 01:10

3 Answers3

3

You are talking about integers. Just copy them by leaving out the reference notation and all possible changes to a and b are local to the NOD function. There are two usual reasons to use a reference. One, you wish to change them (but you don't need to here). Second, sometimes a reference offers more performance than a copy. But that's not the case here because integers are about as cheap as things get in C/C++.

int NOD(int a, int b) {
    if (b == 0) {return a;}
    const int remainder = a % b;
    return NOD(b, remainder);
}
John Griffin
  • 377
  • 2
  • 8
-1

Your code is invoking Undefined Behavior, after which everything is possible. So, what you're asking is flat out impossible - after UB, even immutable variables are mutable.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Doesn't this only apply if the original value was declared as `const`? – user2296177 Sep 27 '16 at 07:21
  • @user2296177: Indeed. I might have interpreted "it breaks everything" a bit too aggressively. The question was about "immutable variables", and taken literally that [cannot refer to `const int& a`](http://stackoverflow.com/a/2766109/15416). So I presumed `a` was a reference to such an immutable variable `const int Foo = 4;` somewhere. – MSalters Sep 27 '16 at 07:26
-2

You can never prohibit that a piece of code changes its local copy of a primitive data type, that should be clear. And this would be the same in a functional language too.

The C++ way could be to wrap the int into a class that has only one constructor taking an int and only const methods to read the content.

If you make this single constructor inaccessible to "others" than they will not be able to build a changed object on their own.

Or you could return a lambda expression, that returns the int. Internally the compiler would produce an instance of a class on the fly. Performancewise this should be about the same but maybe less clumsy to read.

  • I largely agree that the OP is trying to solve a non-problem, but why the downvotes on this one? – Dan Fego Sep 28 '16 at 01:03
  • 1
    a) There is no local copy here. b) Functional languages are irrelevant here. c) The part about the "C++ way" doesn't prevent writing either. d) The part about lambdas is completely nonsense. – deviantfan Sep 28 '16 at 01:10
  • @DanFego what deviantfan said. The answer gives the wrong impression to someone who doesn't understand the way the language works or what types of guarantees are useful. – xaxxon Sep 28 '16 at 14:16