What does this incremental syntax mean? [duplicate] - c++

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)?

Related

Self-initialization: possible use cases [duplicate]

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?

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 mean "int(i)=1;"? [duplicate]

This question already has answers here:
Why does C++ allow us to surround the variable name in parentheses when declaring a variable?
(2 answers)
Closed 5 years ago.
I am new to C++, I see following syntax in c++ to initialize variable.
int(i)=1;
Then, I have compiled in G++ compiler and compiler did not give any error or warning.
So, What does mean int(i)=1; in C and C++?
Also, I have tested in C, I thought, the C compiler give an error but it's also working fine.
It's basically a strange way to write
int i = 1;
Nothing to worry about.
Sometimes, parenthesis around the variable name are necessary in defintions (eg. pointer to functions), and there is no reason to prohibit them for other cases, so it's allowed without any deeper reason.
Maythe the author didn't like spaces (such people exist).

warning: taking the address of a label is non-standard [duplicate]

This question already has answers here:
Error: Label used but not defined when using && operator [closed]
(3 answers)
Closed 8 years ago.
Can anyone explain how the following code creates a label?
char memory[] = "hello";
&&memory[0];
error: label 'memory' used but not defined
&&memory[0];
That's not valid C++, thus a conforming extension can assign any semantics one could want to it.
It so happens, that &&label is the GNU folks' way of taking the address of a label for computed goto's, a GNU extension.
That's it.
Reference: https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html

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.