Self-initialization: possible use cases [duplicate] - c++

This question already has answers here:
Why is initialization of a new variable by itself valid? [duplicate]
(1 answer)
What's the behavior of an uninitialized variable used as its own initializer?
(3 answers)
Closed 9 months ago.
This post was edited and submitted for review 9 months ago and failed to reopen the post:
Original close reason(s) were not resolved
C++ allows self initialization.
I understand lines like the following result in j's value being unspecified or just in undefined behavior:
int j = j;
However, the following self-initialization is fine:
void* myAddr = &myAddr;
My questions are:
As I can't come up with any, can anybody provide an example where a self initialization like the latter may actually be useful?
Is there any other self initialization that doesn't result in undefined behavior nor in the variable's value being unspecified? I.e. is there any other well-behaved self initialization?

Related

What happens if I intentionally do not return from a non-void function? [duplicate]

This question already has answers here:
What happens when a function that returns an object ends without a return statement
(3 answers)
Omitting return statement in C++
(3 answers)
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Closed 1 year ago.
I am trying to write code with expression templates that will permit me to perform operations on objects of different types, and I am trying to customize that behavior. For this purpose, it would be convenient for me if I could write a function such as:
double ReturnVal() {};
This code produces a compiler warning "no return statement in function returning non-void". But - the function appears to do what I want. Specifically, if I run:
double x = 5;
x = ReturnVal();
I find that x remains 5. It is unchanged by the function ReturnVal(). This is what I want the code to do.
I know this seems odd, but again, this would be useful because I am trying to write code with templates, and customizing the behavior of operators when they operate on similar (but different) types objects.
So, the question is - can I rely on this behavior that "x = ReturnVal()" doesn't change x? That's what I want, and it's what I am seeing gcc do in my testing. But I am nervous that this may be undefined behavior, and I can't rely on it.

C++ correct references [duplicate]

This question already has answers here:
Whats the difference between these two C++ syntax for passing by reference? [duplicate]
(1 answer)
Placement of the asterisk in pointer declarations
(14 answers)
Closed 4 years ago.
Can someone explain what the difference is, if any, between these two lines:
int& i;
int &i;
I know these are both references and both seem to work fine. Is there a reason to use one over the other? Is there any rule saying what is the right way?
Thanks in advance.
There is absolutely no difference in meaning between these two, it is a purely stylistic matter. Just pick one and try to be consistent within a project.
I believe the examples in the language standard put the & symbol on the left - that's as good a reason as any to prefer one way over the other, I suppose.
That said, as you've written it, neither line is valid code, because you can't have an uninitialised reference. You would need something like:
int a = 10;
int& b = a;

What does this incremental syntax mean? [duplicate]

This question already has answers here:
scope resolution operator without a scope
(6 answers)
Closed 5 years ago.
I came across a line of code that is quite confusing to me. I tried searching but wasn't sure what to even search for.
The line is as follows:
int i = ++::i;
I am lost to whether this line evaluates to:
int i+= i ;
Any help would be appreciated
In
int i = ++::i;
the :: is there to tell the compiler use the i from the global scope. Without it the compiler is going to use the i you just declared which is undefined behavior. For more on that see Using newly declared variable in initialization (int x = x+1)?

Is multiple assignment expression (a=123, b=456, c=789) well-defined? [duplicate]

This question already has answers here:
How does the Comma Operator work
(9 answers)
Closed 7 years ago.
Edit: Apologies for posting a duplicate and such a basic question! I thought it was a trickier one than it actually was, and failed to discover the cited article in my original search.
For the language standard experts out there, is the value of an expression like:
(a=1, b=2, c=3)
... defined? From my tests, it appears that all of our compilers evaluate this expression as 3 (GCC, MSVC, Clang).
However, I'm not sure if we should lean on this behavior. I have no intention of writing code like this, but encountered some obscure code which was leaning on this behavior with multiple assignment in a conditional, and was wondering if it was well-defined.
If a, b, and c are all already defined, you are using a comma operator, which would return the value of c = 3, which is 3.
So to answer your question, yes, it is well defined.

What does (void)variableName mean [duplicate]

This question already has answers here:
Why cast an unused function parameter value to void?
(2 answers)
Closed 8 years ago.
I saw following code few times
void func(SomeTypeEGInt varname) {
(void)varname;
}
I wish to know what it means and why people implement such functions.
It tell the compiler that those variables are unused. It is used to prevent the warnings which you will get.
The (void)varname; pattern is typically used to silence compiler warning about unused arguments. So this example is actually an empty function which does nothing.