Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>I can mostly see how to hack together a solution with templates and operator overloading, and probably get much of what I want. I'm hoping, however, that there is a solution out there whose creators have already done the hard work and explored the corner cases and made the appropriate compromises between convenience and function.</p> </blockquote> <p>In my experience those compromises vary depending on the type and application. For example, whether construction from and/or conversion to base type are implicit, whether it's meaningful to use bitwise operators on two values of the type, or logical operators, pre/post incrementing/decrement, <code>+=</code> <code>*=</code> etc.. Some of these things may be able to be optionally and concisely specified using CRTP, or a type-creation preprocessor macro that accepts optional arguments with other macros representing groups of such functions. I have done this kind of thing before in a couple different contexts, but am not aware of any freely available and similarly flexible reusable library. If you are implementing this using macros, it's nice that you can thereby specify the class name to create a unique type. (For an example, see the BENUM library in the boost vault - <a href="http://www.boostpro.com/vault" rel="nofollow">http://www.boostpro.com/vault</a>). Boost's BOOST_STRONG_TYPEDEF has this benefit but lacks flexibility.</p> <p>If you use templates directly, as in <code>template &lt;typename T&gt; class Unique { ... };</code>, then you'll find you can't create two distinct types with the same underlying (template parameter) type. You can resolve this by passing some unique number to the template, as in <code>template &lt;typename T, int Id&gt;</code>. This requires manually providing unique ints each time you instantiate the template - a maintenance pain. If you must automate this allocation and all usage is within one file, you can supply <code>__LINE__</code> (or have a macro do it), otherwise perhaps template on const char* as well and give it <code>__FILE__</code> or look for a compiler-specific preprocessor extension to generate suitably unique numbers/ids.</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