d - What is the difference between immutable and const member functions? -


the d programming language reference shows 2 examples in declarations , type qualifiers section, these both possible:

struct s {     int method() const     {         //const stuff     } }  struct s {     int method() immutable     {         //immutable stuff     } } 

from docs:

const member functions functions not allowed change part of object through member function's reference.

and:

immutable member functions guaranteed object , referred reference immutable.

i've found this question, answers talking data types, not storage classes. same goes d const faq, though it's interesting read.

so difference between 2 definitions above? there expressions can replace //const stuff , legal not //immutable stuff?

immutable methods may called on immutable objects. can work guarantee* object (this) not change, ever.

const methods may called on const, immutable, or mutable objects. guarantee not change object, other references may change object.

i'd go const unless have reason need immutable, const functions callable 3 mutability storage classes.


* @ type system level, anyway. mutating immutable object possible, causes undefined behavior.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -