+ 4
Why is it not working as expected
So in this code, I'm trying to make "i" a const inside the loop but seems not to be working. My IDE already warned me that it has no effect and I'm unable to understand the reason for that. Ps: I know other method to achieve the same result but I'm demanding for an explanation on why this won't work https://code.sololearn.com/cbMHW7lcS87P/?ref=app
15 Réponses
+ 3
After taking deep look into the issue, I observed that "I" is the type of int&. To make it a reference to a const, or have the same behavior as
const int&
const auto&
I have to declare "i" has
decltype(v)::const_reference
+ 2
Alhaaj I've since unlocked that badge... I did not benefit any badge from this question
+ 1
Runtime Terror
it works, but you're only adding codes that's going to be compiled away in the end.
I usually just stick an auto and call it done.
for(auto i:v){
i *= 2;
cout<<i<<' ';
}
use https://godbolt.org/ to see the assembly output of your code and compare it with the one simply using auto. There is no advantage in doing it the long way.
+ 1
Bob_Li actually what I'm trying to do here is
for(const auto& I: v)
where "i" is a const and preventing the line that says
i *= 2
Which further leads to a compile time error. But the code just run unexpectedly.
I'm using an IDE offline and it says the "const" is unnecessary when used like this, but there's no reason for that
+ 1
Bob_Li I don't think it's an iterator... Since I declared it as
decltype(v)::reference
it should be int&.. the iterator format should be
decltype (v)::iterator
decltype (v)::const_iterator
To my understanding, I think type of "I" should work the same just like
const auto& | const int&
But I'm obviously missing something here
+ 1
Bob you don't understand my question... I'm actually asking why this particular won't work as expected even after I declared it as "const" explicitly
I'm not expecting this program to run, but it does which is the reason for this question
I'm expecting a compile time error due to the const
+ 1
Yeah now you get it
+ 1
It's not about the longest or shortest, the standard encouraged the use of auto& instead or even better "decltype" to retain top level const.
But in this I'm just trying to understand the relationship of decltype with container type aliases
also, in parameter passing, auto will likely not work and it has to be mentioned like this
+ 1
Runtime Terror nice way to unlock new badge (self learner) 😂
+ 1
Runtime Terror just chill bro im just making fun don't take it seriouly :)
0
making i a const int& then trying to modify it with i*= 2 will throw an error. what else would you expect? It is preventing the modification by throwing an error.
0
you can use const int& i but you have to comment out the i *= 2 part. That part becomes the source of your error if i is declared const int&
but really, there is no actual gain in micro-managing your code this way. You're just fighting the compiler.
or if you still want -2 you can do this:
for(const int& i: v) {
int x = i * 2;
cout << x << " ";
}
you just can't modify i anymore if you declare it as const int&
0
you mean why does
const decltype(v)::reference i:v
does not throw an error
while
const int& i: v
throws an error?
because they're not the same.
I think the first one is basically just
int& i:v
The assembly codes are the same in the godbolt compiler for
for(const decltype(v)::reference i:v)
and
for(int& i:v)
0
that's great. now you have a way to write
const int& i
the long way...😁
0
as long as the compiler understands it, I always go for the easiest way.
The code is just the way people communicates with the compiler. You can do it formally or colloquially.