Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Another question can be asked (or simply another way to look at the original question):</p> <h2>Does making a struct <code>const</code> make all its members <code>const</code>?</h2> <p>If I have:</p> <pre><code>struct whatever { int data; }; const whatever test; </code></pre> <p>Will test.data be <code>const</code> too?</p> <p>My answer is : Yes. If you declare an object of type <code>whatever</code> with <code>const</code> then all its members will be <code>const</code> too</p> <p>Similarly, if you declare an object of type <code>whatever</code> with <code>volatile</code> then all its members will be <code>volatile</code> too, just like if you declare the object with <code>const</code>, all it's member will be <code>const</code> too.</p> <p><code>const</code> and <code>volatile</code> are two faces of the same coin; they're so that the Standard often refers to them as <code>cv-qualifiers</code>.</p> <hr> <p>Quoting from the Standard ($7.1.5.1/8)</p> <blockquote> <p>[Note: volatile is <em>a hint to the implementation</em> to avoid aggressive optimization <strong>involving the object</strong> because <strong>the value of the object</strong> might be changed by means undetectable by an implementation. See 1.9 for detailed semantics. In general, the semantics of volatile are intended to be the same in C + + as they are in C. ]</p> </blockquote> <p>That means, if your object is an instance of a struct, then the compiler cannot <em>avoid aggressive optimization involving the object</em>, <strong>unless</strong> it avoids aggressive optimization of each of it's members. (Otherwise, how else it can avoid optimization involving the object?)</p> <hr> <p>Related topic:</p> <p><a href="https://stackoverflow.com/questions/4437527/why-do-we-use-volatile-keyword-in-c/4437555#4437555">Why do we use volatile keyword in C++?</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload