title: Const Boost Variant with Pointer types in a map
body: <p>i am searching for a way to store different type of pointers in a map without using void* for obvious reasons. I actually know the types of the pointers at compile-time and these pointers as well as their types need to be const while their values need to be changed from times to times. The types are always some kind of numerical types. </p>
  
  <p><strong>Background:</strong>
  The variables behind these pointer are actually always global variables which need to changed if an specific event happens. Eachs event has an Id, the first member of that map, which changes the variable to a value transported by that event as a string.</p>
  
  <p>So far i think boost::variant would do the trick. I am new to variant and i dont know whether the following would work as expected regarding the constness, but i assume after reading the f*cking manual that it should be ok. The main issue still is how to change that value behind that pointer utilising the standard conversions:</p>
  
  <pre><code>class Data{
      public:
      typedef boost::shared_ptr&lt;Data&gt; Ptr;
      typedef boost::variant&lt;double*, float*, unsigned int*, int*, unsigned short*, short*, unsigned char*, char*&gt; PodPointerVariant;
      double factor;
      const PodPointerVariant varPointer;
  
      Data(PodPointerVariant variable) : 
          factor(0.0),
          varPointer(variable) {}
  }   
  
  std::map&lt;unsigned int, Data::Ptr&gt; dataMap;
  unsigned int intValue;  
  float floatValue;
  
  void main()
  {
      intValue = 1;
      Data::Ptr newEntry(new Data(&amp;intValue));
      newEntry-&gt;factor = 1.1;
      dataMap-&gt;insert(std::make_pair(1,newEntry));
  
      // Omitted find key and safety if not found...
      unsigned int eventId = 1;
      *(dataMap[eventId]-&gt;varPointer) = 2.1 * dataMap[1]-&gt;factor; // Should be automagically converted to 2 because the pointer is of type unsigned int, but gives a compiler error? I cant dereference that normally.
  
  }
  </code></pre>
  
  <p>Is there an easy way to dereference like this? Maybe using a visitor class? Or anything else? Ideally Data->varPointer shall only be initialised once and only the value may be changed, like a "double * const", so i gets checked at compiled-time if somebody messes around with that pointer. </p>
  
  <p>Thank you!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>After some trial and error i found out that it indeed works as expected. This is what i've done this far:</p>
  
  <pre><code>template&lt;typename U&gt;
  struct DeRefAssignVisitor : public boost::static_visitor&lt;&gt;
  {
      U x;
      double factor;
      DeRefAssignVisitor(U x, double factor) : x(x), factor(factor) { }
      template &lt;typename T&gt;
      void operator()(T* const p) const
      {
          *p = (T)(x * factor);
      }
  };
  
  class Data{
      public:
      typedef boost::shared_ptr&lt;Data&gt; Ptr;
      typedef boost::variant&lt;double * const, float* const, unsigned long* const, long* const, unsigned short* const, short* const, unsigned char* const, char* const, plcbit* const&gt; PodReferenceVariant;
      double factor;
      const PodPointerVariant varPointer;
  
      Data(PodPointerVariant variable) : 
          factor(0.0),
          varPointer(variable) {}
  
      template &lt;typename T&gt;
      void setPointerValue(T value) { boost::apply_visitor(DeRefAssignVisitor&lt;T&gt;(value, this-&gt;factor), this-&gt;varPointer); }
  }   
  
  std::map&lt;unsigned int, Data::Ptr&gt; dataMap;
  unsigned int intValue;  
  float floatValue;
  
  void main()
  {
      intValue = 1;
      floatValue = 2.111;
      Data::Ptr newEntry(new Data(&amp;intValue));
      newEntry-&gt;factor = 1.1;
      dataMap-&gt;insert(std::make_pair(1,newEntry));
  
      // Omitted find key and safety if not found...
      unsigned int eventId = 1;
      dataMap[eventId]-&gt;setPointerValue(floatValue); // Works like a charme: converting compatible types automagically :-)
  }
  </code></pre>
  
  <p>Templates 4TW :-D Thanks everybody!</p>