0

When declaring a variable as a const and trying to modfiy its value later, you will get a compiler error. For example this code:

void func(){ const int a = 5; a = 4; } 

will generate error C3892 on MSVS. However, if the opposite case happened, no error nor warning will be thrown. For example, this code:

void func(){ int a = 5; std::cout << a; } 

won't produce any warning even with /wall configuration. I know that this code is not buggy. It is just does not follow the best practice.

Why there is no warning for such thing? is it only on MSVS? Does the standard have anything to say about that? Are there another compilers that produce a warning for this?

4
  • 1
    Why do you expect that the second snippet should emit a warning? Commented May 29, 2016 at 20:10
  • I just see it as non-optimal coding which can be caught by the compiler. So why not getting a nice warning about it Commented May 29, 2016 at 20:14
  • 1
    It's legitimate C++ code, although the compiler will most probably optimize it away as std::cout << 5;. Is not the compiler's job to emit warnings for "bad" practices with some exceptions though (e.g., if(A || B && C)) that there's a high probability to shoot your foot. This is most likely a job a for an static analysis tool. Commented May 29, 2016 at 20:19
  • I see. However, marking a variable as const may increase the possibility for the compiler to optimize the code (as I read some where). So it is still logical for the compiler to give you a warning that you would loss some resources by not marking your variable as const. Anyway, as dasblinknlight answered it seems that it all about the compiling time. Commented May 29, 2016 at 20:23

1 Answer 1

1

When declaring a variable as a const and trying to modifiy its value later, you will get a compiler error.

That's right, that's because you try misleading your compiler by telling it that you are not going to modify something, and then modifying it later.

Why there is no warning for such thing?

Because there is nothing misleading about that. Declaring a variable non-const does not tell the compiler that you are going to modify it, only that you want to have an option to modify it later.

Does the standard have anything to say about that?

No. Although the standard talks about the other case (modifying a const), it does not say anything about not modifying a non-const.

One reason for this is that finding a modification of a const is a trivial task, because it happens in one place. However, finding that there are no modifications of a non-const requires the compiler to look through the entire scope of the variable from the point of its declaration on. That is why finding potential const applications is left for program verification and refactoring tools.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.