Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's a tough concept to implement in C++, as you're seeing. I'm always a proponent of using the Boost library, which has <a href="http://www.boost.org/doc/libs/1_38_0/doc/html/variant.html" rel="nofollow noreferrer">already solved it for you</a>. You can typedef the complex boost variant template class to something more usable in your specific domain, so </p> <pre><code>typedef boost::variant&lt; int, float, bool &gt; ParamT; class Param { public: // initialize the variants Param(ParamT min, ParamT max, ParamT value) : m_Min(min), m_Max(max), m_Value(value) {} // example accessor template&lt;typename OutT&gt; const ParamT&amp; value() { return boost::get&lt;OutT&gt;(m_Value); } // other accessors for min, max ... private: ParamT m_Min, m_Value, m_Max; }; Param speed(-10.0f, 10.0f, 0.0f); float speedValue = speed.value&lt;float&gt;(); </code></pre> <p>Now, to add another type to your variant (eg, long, std::string, whatever) you can just modify the typedef of ParamT; The catch, here, is that the burden of checking the types is on you - it'll throw an exception if you store a float and try to receive an int, but there's no compile-time safety.</p> <p>If you want to get really crazy, you can implement an overloaded cast operator on a proxy object....</p> <pre><code>class ProxyValue { public: ProxyValue(ParamT&amp; value) : m_Value(value) {} template&lt;typename ValueT&gt; operator ValueT() { return boost::get&lt;ValueT&gt;(m_Value); } private: ParamT&amp; m_Value; }; </code></pre> <p>You'd return this from a non-templated value() function in Param, instead of the variant itself. Now you can assign a value without the template call..</p> <pre><code>Param speed(-10.0f, 0, 10); float speedValue = speed.value(); </code></pre> <p>Though fair warning, you're stepping into meta-programming hell here. Here thar be dragons. And as always, this is not a complete solution, just a pointer. YMMV.</p> <p><a href="http://codepad.org/QchqIhH6" rel="nofollow noreferrer">Heres a roughly working version showing how to use it, and the failures that are easy to hit.</a></p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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