const - Does C++ support STL-compatible immutable record types? -


a number of languages have support immutable types – once have constructed value of such type, cannot modified in way. won't digress benefits of such types, has been discussed extensively elsewhere. (see e.g. http://codebetter.com/patricksmacchia/2008/01/13/immutable-types-understand-them-and-use-them/ )

i create lightweight immutable record type in c++. obvious way of doing const members. example:

struct coordinates {     const double x;     const double y; }; 

unfortunately, type declared in way doesn't support copy assignment, , such can't used stl containers, pretty fatal drawback. don't think there way around this, grateful know if can see one.

for it's worth, far can see, c++ conflating 2 things: a) whether can assign new value variable, , b) whether "values" (=objects) can modified after constructed. distinction clearer in e.g. scala, 1 has

  • val vs var: var can have new value bound it, val cannot
  • immutable , mutable objects, particularly collections

so can write following:

val val_mutable = collection.mutable.map(1 -> "one") val val_immutable = collection.immutable.map(1 -> "one") var var_mutable = collection.mutable.map(1 -> "one") var var_immutable = collection.immutable.map(1 -> "one") 

vars can rebound point @ other values:

//fails: val_immutable = collection.immutable.map(2 -> "two") //fails: val_mutable = collection.mutable.map(2 -> "two") var_mutable = collection.mutable.map(2 -> "two") var_immutable = collection.immutable.map(2 -> "two") 

mutable collections can modified:

val_mutable(2) = "two"     //fails: val_immutable(2) = "two"     var_mutable(2) = "two"     //fails: var_immutable(2) = "two"     

in c++, consting data members of type makes immutable type makes impossible create "var"s of type. can can see reasonably lightweight way of achieving former without latter? [please note "lightweight" part -- in particular, not want create accessor function every element of struct, results in massive drop in readability.]

you mention can't use coordinates class stl containers written const members. question of how answered (inconclusively) here:

https://stackoverflow.com/a/3372966/393816

the same suggestion, make instances const rather members, has been made in comment threads on question too. agree comment less reliable since must change more code, perhaps use typedef so:

struct mutablecoordinates {   double x;   double y; };  typedef const mutablecoordinates coordinates; 

so users of coordinates objects automatically immutable version unless explicitly it? offset of worry verbosity of using struct safely.

of course, can switch reference semantics using shared_ptr, work const objects since different references can't mess each other, comes overhead of manual memory allocation rather excessive struct of 2 doubles.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -