Can the constness be changed in the middle of the program?

  • Context:
  • Thread starter Thread starter ORF
  • Start date Start date
  • Tags Tags
    Program
Click For Summary

Discussion Overview

The discussion revolves around the handling of const variables in C++11, specifically whether it is possible to change a const variable during program execution. Participants explore various methods and implications of modifying const variables, addressing both theoretical and practical aspects.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant expresses a need to change a const variable and questions if it can be converted to a non-const variable.
  • Another participant asserts that labeling a variable as const prevents modification and suggests that passing const variables by reference should not allow changes.
  • A different participant raises the possibility of using pointers and improper casts to access variables, providing an example of unsafe casting.
  • One participant clarifies the distinction between const and a user-defined CONST, emphasizing the importance of understanding the context of these terms.
  • Multiple approaches to modifying const variables are proposed, including C-style casts, C++ style casts using const_cast, and using mutable members in classes.
  • Another participant mentions the use of unions as a method to modify const variables, although they caution about the implications of such practices.

Areas of Agreement / Disagreement

Participants express differing views on the safety and appropriateness of modifying const variables. While some suggest methods to achieve this, others caution against the potential for undefined behavior and emphasize the importance of adhering to const correctness.

Contextual Notes

Participants note that casting away const-ness can lead to undefined behavior, and there are concerns about compiler-specific handling of mutable members. The discussion reflects a range of practices and opinions on the topic without reaching a consensus.

Who May Find This Useful

This discussion may be useful for C++ programmers dealing with const correctness, those troubleshooting bugs related to variable modification, and individuals interested in the implications of casting in C++.

ORF
Messages
169
Reaction score
19
Hello

I'm using C++11. I had a bug (about one variable which changed at some point ), so in order to find out where it is I pass all const arguments by reference (to all functions of the program). At one point one variable must be changed, and I wonder if there is a way to convert that const variable into a non-const one.

Thank you in advance

Greetings
 
Technology news on Phys.org
Labeling a variable as CONST is a preprocessor command.
This does a couple things.
First off it tells the compiler that if the code tries to change the value of that variable to throw an error.
Secondly (I think) it changes where, in the memory allocated to your program, that variable is stored.

I'm a little surprised honestly that the compiler would let you pass a CONST variable by reference,

if a variable has to be changed it is by definition NOT a CONST and you shouldn't label it as such. There is no safe way that I am aware of to change a value of a CONST.
 
  • Like
Likes   Reactions: ORF
Do you ever take the address and use a pointer to access the variable(s)? Maybe you have an improper cast?

const char foo[] = "hello";
char *bar = (char*) foo;
strcpy(bar, "world"); // Oops!
 
  • Like
Likes   Reactions: ORF
cpscdave said:
Labeling a variable as CONST is a preprocessor command.
The OP was asking about const rather than CONST. The former is a keyword in C and in C++. The latter, who knows what that means? Unless the programmer has #defined CONST to mean something, even the preprocessor doesn't know what CONST means. If you worked for me and you insisted on doing this, I would think of firing you unless you had a very good reason for doing so. For example, having to deal with a compiler last updated in the previous millennium would qualify as a justifiable excuse.
ORF said:
Hello
I'm using C++11. I had a bug (about one variable which changed at some point ), so in order to find out where it is I pass all const arguments by reference (to all functions of the program). At one point one variable must be changed, and I wonder if there is a way to convert that const variable into a non-const one.

There are multiple ways of achieving this. None are all that good.

Approach #1: Use a C-style cast to remove the const qualifier:
C:
void some_function(const SomeType& ref) {
    ((SomeType&)ref).something_i_want_to_change = 0;
}

Approach #2: Use a C++ style cast to remove the const qualifier:
C:
void some_function(const SomeType& ref) {
    const_cast<SomeType&>(ref).something_i_want_to_change = 0;
}

Approach #3: (Applicable to member data only): Mark a data member as mutable:
C:
class Foo {
public:
    some_function () const {
        something_i_want_to_change = 0;
    }
private:
    mutable int something_i_want_to_change;
};

The first is very hard to find. Those C-style casts that cast away const-ness are just bad. The latter two are easily found via grep.

Beautiful programming concepts aside, there are times when you either need to cast const-ness away (with a const_cast, not a C-style cast), or just magically making the const-ness disappear, which is what mutable does. Casting away const-ness is, by definition, undefined behavior. Marking a member as mutable (supposedly) is well-defined behavior. Beware, however, that a number of compilers do not treat mutable properly.
 
Last edited:
  • Like
Likes   Reactions: newjerseyrunner, ORF, Silicon Waffle and 1 other person
In C, you are allowed to weird things. On your own head be it! But, you could use a union:
Code:
union Uconst {
  const int c_I;
  int I;
  };
This allows you to declare a constant and modify it using another name...
 
  • Like
Likes   Reactions: Silicon Waffle and ORF
Thank you for all the clarification, specially to @D H ;)

Greetings!
 

Similar threads

Replies
5
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 89 ·
3
Replies
89
Views
6K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
38
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
6K
Replies
3
Views
2K