Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there some ninja trick to make a variable constant after its declaration?

I know the answer is 99.99% no, but I figured it was worth a try, you never know.

void SomeFunction(int a) {     // Here some processing happens on a, for example:     a *= 50;     a %= 10;     if(example())        a = 0;     // From this point on I want to make "a" const; I don't want to allow     // any code past this comment to modify it in any way. } 

I can do something somewhat similar with const int b = a;, but it's not really the same and it creates a lot of confusion. A C++0x-only solution is acceptable.

EDIT: another less abstracted example, the one that made me ask this question:

void OpenFile(string path) {     boost::to_lower(path);     // I want path to be constant now     ifstream ... } 

EDIT: another concrete example: Recapture const-ness on variables in a parallel section.

like image 494
Thomas Bonini Avatar asked Sep 08 '10 15:09

Thomas Bonini


People also ask

How can you declare a variable as constant?

Variables can be declared as constants by using the “const” keyword before the datatype of the variable. The constant variables can be initialized once only. The default value of constant variables are zero.

How do you set a variable to constant in C++?

To declare a constant variable in C++, the keyword const is written before the variable's data type. Constant variables can be declared for any data types, such as int , double , char , or string .

How do you modify a constant variable?

The constant variable values cannot be changed after its initialization. In this section we will see how to change the value of some constant variables. If we want to change the value of constant variable, it will generate compile time error.

What keyword is used to declare a variable constant?

Constants are basically variables whose value can't change. In C/C++, the keyword const is used to declare these constant variables. In Java, you use the keyword final .


2 Answers

You could move the code to generate a into another function:

int ComputeA(int a) {   a *= 50;   a %= 10;   if (example())     a = 0;   return a; }  void SomeFunction(const int a_in) {   const int a = ComputeA(a_in);   // .... } 

Otherwise, there's no nice way to do this at compile time.

like image 31
bdonlan Avatar answered Oct 06 '22 01:10

bdonlan


One solution would be to factor all of the mutation code into a lambda expression. Do all of the mutation in the lambda expression and assign the result out to a const int in the method scope. For example

void SomeFunction(const int p1) {    auto calcA = [&]() {     int a = p1;     a *= 50;     a %= 10;     if(example())        a = 0;     ..     return a;   };   const int a = calcA();   ... } 

or even

void SomeFunction(const int p1) {    const int a = [&]() {     int a = p1;     a *= 50;     a %= 10;     if(example())        a = 0;     ..     return a;   }();   ... } 
like image 131
JaredPar Avatar answered Oct 05 '22 23:10

JaredPar