title: variant variable interaction with a non-variant type
body: <p>I'm trying to solve the following problem. I have a vector (it's a custom structure actually, but a vector is a good enough substitute for this issue) of pointers to a custom class <code>A</code>. Class <code>A</code> can actually store either a <code>type_a</code> pointer or a <code>type_b</code> pointer (these types are quite different and are not related to each other). Right now that's implemented by keeping both, setting them to <code>NULL</code> and then having a bunch of <code>if/else</code> statements later on to check which type it is and perform appropriate action.</p>
  
  <pre><code>class A {
  public:
    A() : p1(NULL), p2(NULL) {}
  
    type_a * p1;
    type_b * p2;
  };
  
  std::vector&lt;A *&gt; v;
  
  ...
  
  if (v[0]-&gt;p1 != NULL) { /* do this */ }
  else if (v[0]-&gt;p2 != NULL) { /* do that */ }
  </code></pre>
  
  <p>I plan to add more pointers to class <code>A</code>, and so the above is starting to become a hassle. The solution that I'm currently trying to make work is using <code>boost::variant</code> instead, to have:</p>
  
  <pre><code>class A {
  public:
    boost::variant&lt;type_a*, type_b*&gt; p;
  };
  </code></pre>
  
  <p>The problem I have encountered though, is that one of my actions involves calling a function that would assign some values to a variable depending on what type of <code>p</code> I have. This is what it is now and the appropriate <code>process_stuff</code> function is called inside one of the above <code>if/else</code> statements:</p>
  
  <pre><code>class B { /*...*/ };
  
  void process_stuff(type_a * p, B * b) {
    b-&gt;m_var = p-&gt;fn1();
  }
  
  void process_stuff(type_b * p, B * b) {
    b-&gt;m_var = p-&gt;fn2();
  }
  </code></pre>
  
  <p>I can't get this to work with <code>boost::static_visitor</code> since (as far as I understand) I can't have a non-variant type as an argument in binary visitation, nor can I have a non-const <code>operator()</code> to make the second variable a member of the visitor class and modify that inside <code>operator()</code> with unary visitation. So I'm confused how to convert the above <code>process_stuff</code> function to play along with <code>boost::variant</code>.</p>
  
  <p>Btw I'm not attached to <code>boost::variant</code> and would take other solutions.</p>