title: How to Change a C++ Class member's Type
body: <p>Is it possible to be able to assign a "flexible" type to a class member? This would be somewhat like <code>std::variant</code> or <code>std::any</code>, except <code>std::variant</code> requires types to be stated in advance and <code>std::any</code> requires <code>any_cast&lt;Type&gt;</code> (which isn't a problem, except for saving the type information so I'd know what type to cast the <code>std::any</code> member to). </p>
  
  <p>What I'm looking for is something that makes the following functionality possible:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>class FlexType
  {
      ChangeableType m_Value;
  
  public:
      FlexType(SomeType init_value) 
          : m_Value(init_value)
      {}
  
      void setValue(SomeOtherType new_value) { m_Value = new_value; }
      CurrentType getValue() const { return m_Value; }
  };
  </code></pre>
  
  <p><strong>Edit</strong><br />
  I need the functionality for storing a return type of an <code>std::function</code>, where I want to be able to change the <code>std::function</code>'s return type to any given type. This comes from a class that has a <code>virtual void run() const</code> method (call it "ParentClass" for convenience). I then have a child class that overrides the <code>run()</code> method and runs an <code>std::function</code> and saves the return value in another class member.</p>
  
  <p>It looks something like this:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>class ParentClass
  { // Example basic parent class
  private:
      /* ... Class members ... */
  
  public:
      ParentClass() = default;
  
      virtual void run() const { /* Do stuff w/ class members */ }
  };
  
  template&lt;class FuncRetTy, class... FuncArgs&gt;
  class ChildClass : public ParentClass
  {
  private:
      using function_return_type = FuncRetTy;
      using function_arg_types = FuncArgs...;
      using function_args_container = std::tuple&lt;function_arg_types&gt;;
      std::function&lt;function_return_type(function_args_container)&gt;
  
      mutable function_return_type m_StoredValue;
      function_type m_Function;
      func_args_container m_Args;
  
  public:
      FunctionMenu(
          function_return_type type_var, // Used to deduce return type
          function_type function,
          function_arg_types arguments)
          : m_Function(function)
          , m_Args(std::make_tuple(std::forward&lt;FuncArgs&gt;(arguments)...))
      {}
  
      void run() const override
      {
          m_StoredValue = m_Function(m_Args);
      }
  
      [[nodiscard]] function_return_type getStoredValue() const
      {
          return m_StoredValue;
      }
  
      // This is where my problem is...
      // I want to be able to set m_Function to another function,
      // but this means the return type can change, and so can
      // the tuple template parameters for m_Args.
      // The below code is just pseudocode
      template&lt;class NewFuncRetTy, class... NewFuncArgs&gt;
      void setFunction(
          NewFuncRetTy type_var,
          std::function&lt;NewFuncRetTy(std::tuple&lt;NewFuncArgs...&gt;)&gt; function
          NewFuncArgs... args)
      {
          // Change all class typedef's to new types
          // Reassign m_Function = function
          // Reassign m_Args = make_tuple(std::forward&lt;NewFuncArgs&gt;(args)...);
          // Delete whatever value m_StoredValue already has and 
          //     change type of m_StoredValue to NewFuncRetTy so
          //     getStoredValue() knows what type to return
      }
  };
  </code></pre>
  
  <p>I'm sorry about there being a lot of code, but I figured explaining in detail what exactly I'm asking for would help people understand the question better.</p>