Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>"what is the exact behaviour?"</p> <p>The problem is that <code>push_back</code> takes a reference parameter. You can use the <em>value</em> of a <code>static const int</code> member variable without providing a separate definition of the object, but you can't use a reference to the object itself (since it doesn't exist). The meaning of "using" the member itself is defined in the section of the standard on the One Definition Rule, 3.2/2.</p> <p>One fix is to provide a definition in exactly one translation unit:</p> <pre><code>const int check::init; </code></pre> <p>If you do this, you can also choose to move the <code>= 1</code> initialization from the declaration (inside the class) to the definition (outside the class).</p> <p>Another fix is to create a temporary from the member variable (this only uses the value, it doesn't care where the object is located and hence doesn't care whether it exists), then pass a reference to the temporary:</p> <pre><code>dummy.push_back(int(init)); </code></pre> <p>Of course there's a potential maintenance issue there, that if the types of <code>init</code> and <code>dummy</code> both change to, say, <code>long long</code>[*], and the value changes from <code>1</code> to something bigger than <code>INT_MAX</code>, then you're in trouble. For that reason you could use <code>+init</code>, since the unary + operator also creates a temporary for its result. Readers and future maintainers might be a bit puzzled by it, though.</p> <p>[*] Supposing your implementation has <code>long long</code>.</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. 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.
 

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