Motivation Const variables Exercise Homework 13. Const correctness Ján Dugáček February 10, 2019 Ján Dugáček 13. Const correctness Motivation Const variables Exercise Homework Table of Contents 1 Motivation 2 Const variables Const variables Const objects Violating constness Remarks 3 Exercise 4 Homework Ján Dugáček 13. Const correctness Motivation Const variables Exercise Homework Motivation If a variable isn’t intended to be changed, it’s better to mark it so in order to prevent someone else who edits the code from changing it i n t doStuff ( const i n t x ) { // . . . x = x % 12; // Error , x i s const // . . . r e t u r n x + r e s u l t ; // We can be c e r t a i n t h i s i s the o r i g i n a l value } Large objects can be passed as reference (std::string&) to prevent unnecessary copies and a const reference (const std::string&) ensures it’s not edited inside the function Ján Dugáček 13. Const correctness Motivation Const variables Exercise Homework Const variables Const objects Violating constness Remarks Const variables const f l o a t a = 42; const unsigned i n t b = 9; std : : pair c{ 3 , 5}; c . f i r s t = 2; // Error , the f i r s t i s const c . second = 2 . 0 ; // The second i s not const const std : : pair d{ 3 , 5}; d . second = 1 5 . 1 7 ; // Error : The whole o b j e c t i s const std : : pair e = d ; // Const can be copied i n t o non−const std : : pair & d = e ; // Error : Cannot make non−const r e f e r e n c e to const There is no way to accidentally change something that is const Anything can be converted into const Ján Dugáček 13. Const correctness Motivation Const variables Exercise Homework Const variables Const objects Violating constness Remarks Const objects const std : : s t r i n g t e x t ( " Free beer " ) ; i n t found = t e x t . f i n d ( " " ) ; // Okay , s t r i n g not modified std : : s t r i n g beer = t e x t . s u b s t r ( found + 1 ) ; t e x t [ found ] = ’_’ ; // Error , s t r i n g i s modified How does the compiler know which methods modify the object and which ones don’t? Ján Dugáček 13. Const correctness Motivation Const variables Exercise Homework Const variables Const objects Violating constness Remarks Const objects #2 s t r u c t importantData { i n t p r e c i o u s ; i n t& g e t P r e c i o u s () const { p r e c i o u s = 2; // Error , we are i n a const o b j e c t std : : cout << p r e c i o u s << std : : endl ; // Okay r e t u r n p r e c i o u s ; // Error , t u r n i n g i n t o a nonconst r e f e r e n c e } }; Methods can be declared const, which allows them to be called if the object is const, but all member variables are const inside them Ján Dugáček 13. Const correctness Motivation Const variables Exercise Homework Const variables Const objects Violating constness Remarks Const objects #3 c l a s s importantData { i n t p r e c i o u s ; p u b l i c : i n t g e t P r e c i o u s () const { r e t u r n p r e c i o u s ; } // Returned value i s a copy i n t& g e t P r e c i o u s () { r e t u r n p r e c i o u s ; } // E d i t i n g the r e t u r n value e d i t s the member void s e t P r e c i o u s ( i n t s e t ) { p r e c i o u s = s e t ; } }; There can be const and nonconst versions of functions chosen accordingly to the constness of the object Ján Dugáček 13. Const correctness Motivation Const variables Exercise Homework Const variables Const objects Violating constness Remarks Violating constness c l a s s importantData { i n t p r e c i o u s ; mutable i n t imprecious ; p u b l i c : i n t g e t P r e c i o u s () const { imprecious = p r e c i o u s − 1; // Okay , mutable r e t u r n p r e c i o u s ; } }; Some objects hide some internal attributes that don’t affect their external behaviour but serve for some other purposes, like caching, synchronising and other stuff, these attributes have to be marked with keyword mutable (use it only if this is the case) Ján Dugáček 13. Const correctness Motivation Const variables Exercise Homework Const variables Const objects Violating constness Remarks Utterly violating constness c l a s s importantData { i n t p r e c i o u s ; mutable i n t imprecious ; p u b l i c : i n t g e t P r e c i o u s () const { imprecious = p r e c i o u s − 1; r e t u r n p r e c i o u s ; } void specialDebugFunction ( i n t s e t ) const { const_cast( p r e c i o u s ) = s e t ; } }; You can modify a const variable by removing its constness using const_cast, but this is needed very rarely Ján Dugáček 13. Const correctness Motivation Const variables Exercise Homework Const variables Const objects Violating constness Remarks Remarks Constness doesn’t serve to limit you, its purpose is to protect yourself from making mistakes You should use it to make sure you don’t break your code when you return to it a year later and protect others from using your code incorrectly In very rare cases, compiler uses the knowledge that something is const to make some optimisations It’s rather important when methods accepts references to objects, as it’s the only way to ensure they don’t modify the variables in the function that called it It’s crucial when using more threads (usually to leverage more processor cores), because more cores can read a variable simultaneously, but cannot write into it simultaneously Ján Dugáček 13. Const correctness Motivation Const variables Exercise Homework Exercise 1 Create a class that gives access to lines in a file, but reads the (possibly gigabytes long) file only up to the line that’s requested 2 Create a SortedVector class that encapsulates a vector of some numbers and has a method to sort it 3 Create a Collection class that allows reading numbers, adding them and changing them only when non-const and doesn’t alow removing any 4 Create a Factoriser class that can factor integers and remember all integers it has already factorised to make it factor faster Use const wherever there’s any reason to use it Ján Dugáček 13. Const correctness Motivation Const variables Exercise Homework Advanced exercise 1 Create a class that reads a file by chunks of 16 bytes and holds them in a vector, allowing a const access and a non-const access that saves the changes to disk when done (returns some kind of wrapper whose destructor makes the class update the object) 2 Create a Decimator class that holds numbers representing some data, each with time and value, and has a method to remove the one that will produce data with the smallest possible mean square difference Use const wherever there’s any reason to use it Ján Dugáček 13. Const correctness Motivation Const variables Exercise Homework Homework Write a class that represents some measured data as a vector of pairs of time and value, has a method to return an interpolated value at some time between known values and caches recently interpolated values so that it would not have to compute them again if requested again shortly later You have two weeks to do it You must keep const correctness (and use mutable where it makes sense) Ján Dugáček 13. Const correctness