Const and non-const in one list of variable definitions in C ++

Why is it impossible to define const and non-const variables in a single definition list ?

For example, when using a loop for:

for (size_t i = 0, const iCount = data.size(); i < iCount; ++i);

It would be better in terms of matching const-correctness, I think.

PS The first time I asked this question incorrectly. This is more for language developers, not why can it be compiled on my machine? . I wanted to say that it would be much better if it were implemented as syntactic sugar in the following C ++ standards. It will also prevent calls data.size()in each iteration of the loop. I mean, why not expand the current list of variable definitions for C ++ support const, and non-constin the same list of definitions?

+4
source share
5 answers

I wanted to say that it would be much better if it were implemented as syntactic sugar in the following C ++ standards.

" ".

"" , , , , IDE , , , .

data.size() .

, ? , , -, ?

, ++ const non-const ?

, for, :

{
  const size_t iCount = data.size();
  for (size_t i = 0; i < iCount; ++i)
    ;
}
+1

, for . const .

+2

-, . , .

cv- . ,

int x, * const p = &x;

,

int x, const y = 10;

.

isocpp.org, ++.:)

+2

Inside foryou can declare several variables provided that they are of the same type. size_tand const size_t- different types.

You do not need to declare iCountthere, just write ; i < data.size();as a condition.

0
source

Because they are different data types and, therefore, this is not one list of definitions.

0
source

All Articles