Can someone explain this?

Can someone explain this to me please?
I'm not 100% sure of how this program works

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

void print()
{
	int v[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	
	for (auto x : v)
		std::cout << x << '\n';

	for (auto x : { 10, 21, 32, 43, 54, 65 })
		std::cout << x << '\n';


}


int main(){
	print();
		int keepopen = 1;
		while (keepopen = 1) {};

}


It's probably something really simple, thank you
it makes an int array v with all values from 0 to 9 (int v[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };)
then for all elements in that array (for (auto x : v)) that element and a new-line is printed out (std::cout << x << '\n')

after that it loops through an array that doesn't have a name and loops through that values and prints them + a newline

after that the application freezes in a while(1) loop
Can you elabrate on (auto x : v)

So when defining, ( auto x : v ) you are effectively saying, for every element of v assign x to it and print it out, so in every iteration of std::cout in this program, ovewrites the x variable with the next itteration of element, in the array and prints it? correct?
The same for the next part of the function but instead of defining the array as v, the array is defined expliciltly?
And what is the " : " operator called and are there any other uses for it other than defining something to exist in something else like this? (If i got that right)
for(auto x : v) is actually only available in c++11 and newer standards.
It's a shorter version to loop an STL container

for every element in v assign x to it and do something with it
yeah, that's correct

it is basically equivalent to something like:
1
2
3
4
5
for(int i = 0; i < v.size(); ++i) {
    auto x = v[i]; // copy constructor

    // stuff
}
Note that auto is whatever type is stored in v

for every element of v assign x to it and print it out, so in every iteration of std::cout in this program, ovewrites the x variable with the next itteration of element, in the array and prints it? correct?
Not completely correct.
x is not overwriten, it goes out of scope after each element-iteration and is copy-constructed at the begin of 1 element-iteration


And what is the " : " operator called and are there any other uses for it other than defining something to exist in something else like this? (If i got that right)

I don't know what it's called but the for loop is the only place where it is used.
Last edited on
You can also use ':' operator in bitfields: http://en.cppreference.com/w/cpp/language/bit_field and in ternary conditional.
Last edited on
Thanks for answering my question, one last thing though can you explain what you mean by copy constructed? I've not got that far yet
When you have an Object or intance of a datatype and you want to have a similar Object/instance you can copy construct it:

1
2
3
int a = 5;
int b = a; // CopyConstruction: b has now the value of a
int b(a); // CopyConstruction: b has now the value of a 
@Gamer2015 compiler can just overwrite x variable, in process of optimization, this way it doesn't have to construct new variable, cannot it?
@Gamer2015 compiler can just overwrite x variable, in process of optimization, this way it doesn't have to construct new variable, cannot it?
yeah, he may do it for primitive datatypes but it is not really an optmization.

and as you can see the copy-constructor and the dtor are called in each iteration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Example program
#include <iostream>
#include <string>
#include <vector>

class A {
public:
    A() { std::cout << "ctor" << std::endl; }
    A(const A&) { std::cout << "copy" << std::endl; }
    ~A() { std::cout << "dtor" << std::endl; }
};

int main()
{
    std::vector<A> va = {A(), A(), A(), A(), A()};
    
    std::cout << "for-loop start" << std::endl;
    for(auto a:va) 
    {}
    std::cout << "for-loop end" << std::endl;
}

ctor
ctor
ctor
ctor
ctor
copy
copy
copy
copy
copy
dtor
dtor
dtor
dtor
dtor
for-loop start
copy
dtor
copy
dtor
copy
dtor
copy
dtor
copy
dtor
for-loop end
dtor
dtor
dtor
dtor
dtor
 

http://cpp.sh/4veg

note that you could optimize by using a const reference:

1
2
    for(const auto& a:va) 
    {}
Last edited on
I almost always (unless I need to modify array) use auto&& anyway, and it turns out it does do sth. And btw it's interesting my vs 2015 doesn't print out dtors after loop-end message.
And btw it's interesting my vs 2015 doesn't print out dtors after loop-end message.

you don't have the dtors after for-loop end? O.o

I mean, i would believe it if you'd have no messages between start and end because you just have a reference but the dtors should exist because they are in the vector o.O

I almost always (unless I need to modify array) use auto&&
you should use const auto& if you don't modify it because you can modify auto&&

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Example program
#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::vector<int> vi = {1,2,3,4,5};
    
    for(auto&& a : vi) 
        a = 5;
    for(auto&& a : vi) 
        std::cout << a <<std::endl;
}

5
5
5
5
5

http://cpp.sh/5hae
Last edited on
Aside

This is not such a good example:

1
2
3
int a = 5;
int b = a; // CopyConstruction: b has now the value of a
int b(a); // CopyConstruction: b has now the value of a  


As basic types do not have constructors, int b = a; cannot be copy construction. This is just initialization.

Better minimal example (assuming using namespace std, etc.)

1
2
3
string a = "Hello world";
string b = a; // CopyConstruction: b has now the value of a
string b(a); // CopyConstruction: b has now the value of a  


Andy
Last edited on
And btw it's interesting my vs 2015 doesn't print out dtors after loop-end message.
Never mind, stupid me, I forgot since visual studio doesn't hold console after execution I just didn't see that.
Last edited on
okey good, still note that you can modify a value of type auto&&
@andreywestken, thank you
I wanted to back off while writing this because it was a technically wrong example but I thought it would still serve the purpose
Oh, yeah I wanted to write about that and I forgot. It's been a bad day for me. I thought you cannot modify auto&& since it's r-value reference, but now it makes perfect sense why you can make const auto&&, definetly not only for SFINAE.
there is no advantage over just writing const auto& for iterating a container...
usually you only write auto&& if you want to move from that object because it's an rvalue and therefore not needed anymore so please don't use it for iterating a container.
Last edited on
ok, thanks
P.S. Did you know Gamer2015 ,that when you post piece of code with main() function in code tags you can click on that little gear next to top right corner to run program in cpp.sh? You don't have to bother posting links manually. I appreciate that, but it's not needed :)
Last edited on
No, I didn't know that, thank you!
I'll still post a link under the programms because not everyone knows that :)
No, I didn't know that, ...

I didn't either. How long ago was this feature added? (Just wondering just how dozy I've been!)

Andy
I think it was already here when I created my account so at least 6 months (but probably way longer).
Topic archived. No new replies allowed.