Reference type for SML - sml

I have two refs in SML:
val a = ref 5;
val b = ref 6;
Currently a = b evaluates to false. Is it possible to somehow assign variable b so that a = b evaluates to true?
Thanks

No. It's not, they are pointers to different object.
Technically you could do
val a = ref 5;
val b = a;
That would work.

Related

Does the meaning of the assignment statement in the declaration of a reference is different then after it?

For example:
int a = 0;
int& ref = a;
In this example it took a and add it another name - "ref", and they are the same place at the memory.
In this example:
int a = 0;
int& ref = a;
ref = 5;
it replaced the value of a (so that it replaced "also" the value of ref).
So now the assignment statement gets a new meaning?
When the referent is initialized, it binds itself to an object.
After, you are modifying the bound object, not the reference.
Think of the reference as a pointer you cannot modify once created (but that always points to a valid object, no null reference possible).
The = in the definition of an object is not an assignment; it's an initialization. And, yes, the meaning of an initialization is not the same as the meaning of an assignment.
int a = 0; // initialize a to 0
int& ref = a; // initialize ref to refer to a
ref = 5; // assign 5 to ref; ref is a reference, so this assigns 5 to a
Initialization refers to creating an object (in the example code, an int and an int&; the = sign in an initialization tells the compiler what to use as the initial value of that object.
Assignment refers to setting a new value to an already existing object. So int a = 0; is initialization; a = 3; is assignment.

Does const_cast return the type specified inside angular bracket

I am little confused with the return type of const_cast? Does the type inside the angular brackets <> is the return type?
const int i = 5;
int b = const_cast<int&>(i);
Is const_cast returning int& (integer reference), if yes then why how we storing it in integer? Or we should modify the code to:
const int i = 5;
int & b = const_cast<int&>(i);
Yes. But instead of returning you should say that the type of the resulting expression is the type in <>.
When that type is a reference, as in const_cast<int&>, it means that it is a l-value. In the first case there is no difference, as it is immediately converted to an r-value anyway. But then, the const in such an r-value is ignored, so:
int b = const_cast<int&>(i); //ok
int b = const_cast<int>(i); //also ok
int b = i; //hey! also ok
In the second case there is a difference, because there is no l-value to r-value conversion:
int &b = const_cast<int&>(i); //ok
int &b = const_cast<int>(i); //error: cannot bind a reference to an r-value
int &b = i; //error: cannot bind a `int&` to a `const int&`
Does const_cast return the type specified inside angular bracket
It evaluates to an expression of the type in the angular brackets. What matters here is how you use that expression:
const int i = 5;
int b = const_cast<int&>(i);
In this case, b is just a copy of i. The cast is not required.
const int i = 5;
int & b = const_cast<int&>(i);
Here, b refers to i. Note that using it to modify the value of i is undefined behaviour.
Does the type inside the angular brackets <> is the return type?
Yes, the return type of const_cast<int&>(i) is int&. After that you assign it to a int, and the value get copied.
And in int & b = const_cast<int&>(i);, you assign it to a int&, now b is the reference to i. Pay attention to that any modification on b will cause undifined behaviour.

c++ references appear reassigned when documentation suggests otherwise

According to this question you can't change what a reference refers to. Likewise the C++ Primer 5th Edition states
Once we have defined a reference, there is no way to make that reference
refer to a different object. When we use a reference, we always get the object to
which the reference was initially bound.
However the following code compiles and prints the value 4 which looks to me like the reference was changed?? Please elaborate if this is or is not so.
int a = 2;
int b = 4;
int &ref = a;
ref = b;
cout << ref;
You are not reassigning a reference. A reference acts as an alias for a variable. In this case, ref is an alias for a, so
ref = b;
is the equivalent of
a = b;
You can easily check that by printing out the value of a:
std::cout << a << std::endl; // prints 4
You can understand how references work by comparing their behavior to that of a pointer. A pointer can be thought of as the name of the address of a variable; however a reference is just the name of the variable itself--it is an alias. An alias, once set, can never be changed whereas you can assign a pointer a new address if you want. So you have:
int main(void)
{
int a = 2;
int b = 4;
int* ptr_a = &a;
int& ref_a = a;
ptr_a = &b; //Ok, assign ptr_a a new address
ref_a = &b; //Error--invalid conversion. References are not addresses.
&ref_a = &b; //Error--the result of the `&` operator is not an R-value, i.e. you can't assign to it.
return 0;
}

Can you declare multiple reference variables on the same line in C++?

I.e. are these legal statements:
int x = 1, y = z = 2;
int& a = x, b = c = y;
with the intended result that a is an alias for x, whileb and c are aliases for y?
I ask simply because I read here
Declaring a variable as a reference rather than a normal variable
simply entails appending an ampersand to the type name
which lead me to hesitate whether it was legal to create multiple reference variables by placing & before the variable name.
int x = 1, y = z = 2;--incorrect
int& a = x, b = c = y;--incorrect
The statements should be like this:
int x = 1, y =2,z = 2;
int&q=x,&b=y,&c=y;
All assignment and initialization statements in c++ should be of the following type:
lvalue=rvalue;
here lvalue must always be a variable to which a temporary value/another variable is assigned.
rvalue can be another variable or an expression that evaluates to a temporary variable like (4+5).
You need to append a & on the left of each reference (like you would need a * when you declare a pointer).

Use cases for referencing to a constant variable

I understand the reference variable concept. It's an alias to the other variable.
int varA = 100;
int &varB = varA;
Here varB is a referring to varA, both pointing to same memory location. Changes to one variable reflect in the other.
Question:
a) int &c = 100;
What is the meaning of the above statement, and how does it differ from the following?
b) int c = 100;
Is there any scenario where we need to use 1(a) rather than 1(b)?
All are correct, except this:
int &c = 100; //error
It will give compilation error both in C++03, and C++11. It is because it attempts to bind non-const reference to a temporary object (created out of 100) which is disallowed.
In C++11, you could do this, however:
int && c = 100; //ok
It is called rvalue-reference.
You could bind const reference to a temporary though (both in C++03, and C++11):
int const & c = 100;
-
int c = 100;
It simply defines an object called c and initializes it with 100. No reference here.
int &c = 100
is invalid code, you cannot bind a non-const reference to a temporary.
To make it valid, you need a const reference:
const int &c = 100;
While,
int c = 100
is a valid code. It creates a variable named c of the type int and initializes it with 100.
1) int& c = 100; is illegal, whereas the second one is not.
I understood the reference variable concept.
If you really understood references, are you expecting that after you do c = 101 the constant 100 suddenly turns to 101?
2) No point in answering, since 1) is illegal.
int& i = 100;
Is illegal.
It is legal to say:
int const& i = 100;
const int& i = 100; // same as above
Not particularly useful in this context, but it needs to work for the purposes of function calls:
void foo(int const& i) { ... }
...
foo(100);