Note that there are some explanatory texts on larger screens.

plurals
  1. POOverloading operator=() with callback
    text
    copied!<p>I have a template <code>Property</code> which wraps a data and provides other services. This template is specialized for basic data (float, int_32, bool...) and there exists another specilization for vectors of basic types. Something like:</p> <pre><code>template&lt;typename T&gt; Property : public PropertyBase { public: // Base types specilization. void operator=(T const &amp; data); } template&lt;typename T&gt; Property&lt;std::vector&lt;T&gt; &gt; : public PropertyBase { public: // Specilization for vectors of base types. T const &amp; operator[](std::size_t i) const; } </code></pre> <p>On the specilization for basic data I have overloaded <code>operator=</code> so that a callback is called after the data was changed. On the specilization for vectors I have overloaded <code>operator[]</code> to get indexed access to the ith element of the vector.</p> <p>Now I would like to get a similar behavior that I have for <code>operator=</code> on basic types but with my specialization for vectors. I am stuck though. What I would like to do is something like:</p> <pre><code>Property&lt;std::vector&lt;float&gt; &gt; myProp; myProp[5] = 5.21f; </code></pre> <p>and expect the callback to be called after the assignement happened.</p> <p>The problem is that <code>myProp[5]</code> returns a float, and the callback mechanism is not defined for floats but Property.</p> <p>Of course I can write an indexed setter function such as</p> <pre><code>template&lt;typename T&gt; Property&lt;std::vector&lt;T&gt; &gt; : public PropertyBase { public: // Specilization for vectors of base types. T const &amp; operator[](std::size_t i) const; void set(int i, T const &amp; newValue); } </code></pre> <p>But with the setter the syntax is not as clean as I would expect.</p> <p>I can also call the callback in <code>operator[]</code> but then the callback is called before the assigment, not after.</p> <p>Anyone has an idea to get this clean syntax while still being able to call my callback after the assigment?</p> <p>Thanks!</p> <p><strong>PS:</strong> note that my <code>Property</code> templates are more complex than what is written and do check whether T is a base type or not (which is not done here) but this is not relevant for my question.</p>
 

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