Note that there are some explanatory texts on larger screens.

plurals
  1. POnon-static reference member ‘int& Property<int>::value’, can’t use default assignment operator
    primarykey
    data
    text
    <p>In some C++ code, I am encountering a compile-time error that seems like it might be indicating that I am trying to reseat a reference. But I feel quite sure that I am not trying to reseat a reference. I do not understand the cause of the error. Perhaps I am being silly, and missing something obvious. Or perhaps the problem reflects deep principles of C++ template programming that I do not adequately understand. Either way, I hope some of you can help. This is the code:</p> <pre><code>// p.cpp - slimmed down demonstration of error in prop.cpp template&lt;class T&gt; class Property { protected: T&amp; value; public: explicit Property(T&amp; a) : value(a) { } // default "copy" setter virtual Property&lt;T&gt;&amp; operator=(T a) { value = a; return *this; } // default "copy" getter virtual operator T() const { return value; } template&lt;class U&gt; // must invoke U virtual getter and T virtual setter Property&lt;T&gt;&amp; operator=(const Property&lt;U&gt;&amp; newval) { return (*this = U(newval)); } /* // uncommenting this eliminates the error Property&lt;T&gt;&amp; operator=(const Property&lt;T&gt;&amp; newval) { return (*this = T(newval)); } /**/ }; int main() { //* // this code produces the error { int i_ = 10, j_; Property&lt;int&gt; i (i_), j (j_); j = i; } /**/ /* // this code does NOT produce the error { int i_ = 10; long j_; Property&lt;int&gt; i (i_); Property&lt;long&gt; j (j_); j = i; } /**/ return 0; } </code></pre> <p>When I compile this with <code>gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1</code> using the command <code>g++ -o p p.cpp</code> I get this output:</p> <pre><code>p.cpp: In member function ‘Property&lt;int&gt;&amp; Property&lt;int&gt;::operator=(const Property&lt;int&gt;&amp;)’: p.cpp:4:7: error: non-static reference member ‘int&amp; Property&lt;int&gt;::value’, can’t use default assignment operator p.cpp: In function ‘int main()’: p.cpp:33:13: note: synthesized method ‘Property&lt;int&gt;&amp; Property&lt;int&gt;::operator=(const Property&lt;int&gt;&amp;)’ first required here </code></pre> <p>Before you tell me that I am trying to reseat a reference, please keep in mind that:</p> <ol> <li><p>Uncommenting the definition for <code>Property::operator=</code> that explicitly takes the same constructed type on the right side as the constructed type on the left side eliminates the error. So the problem is not, per se, that <code>Property&lt;int&gt;::value</code> is being accessed through <code>*this</code>.</p></li> <li><p>Independently, as shown in <code>main()</code>, the <code>operator=</code> method template inside the <code>Property</code> class template is instantiated and works fine when the data type of the value on the right side of <code>=</code> is different from the data type on the left side of <code>=</code>.</p></li> <li><p><code>value</code> is a reference, but there is nothing wrong with assigning a value to it with <code>=</code>, at least in general. It is non-<code>const</code>. So once it is instantiated (which is guaranteed, via the initialization list in <code>Property</code>'s only constructor), assigning a value to it does not attempt to reseat it, but instead assigns a new value to the memory location to which it refers.</p></li> </ol> <p>I will provide the larger file <code>prop.cpp</code> which contains (successful) unit tests for each of <code>Property</code>'s member functions, <em>if requested</em>.</p> <p>You may notice that this code is an attempt to (partially) implement "function-like" ("C#-style") properties in C++. But this question is not about whether or not it would actually be appropriate to do this a real-world C++ project, nor is it about whether or not the pattern I have chosen is the most appropriate. It's possible that I'll ask separate questions about that later. If you want to comment on or criticize my approach, I <em>am</em> interested. <strong>I only ask that you not do it here, because it would distract from the more specific purpose of this question.</strong> Instead, you could post your own question, possibly with your own answer, and post a brief comment here linking to your new question.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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