Note that there are some explanatory texts on larger screens.

plurals
  1. POrestricted-use C++ computational types
    text
    copied!<p>I would like to have a way of defining computational types (e.g., somethng that acts like a "limited <strong>int</strong>") that do not automatically convert to their base types or to each other--behavior similar to that of typed pointers.</p> <p>For example, I'd like to be able to define <strong>XCoord</strong> and <strong>YCoord</strong> types to use for X and Y coordinates. I can do that with <strong>typedef,</strong> of course, and it makes my code more explicit, but what I really want is for the compiler to complain when I accidentally pass a Y coordinate as a function call argument when I should have used an X coordinate. Another use case is associating dimensional units with numbers, to get a complaint if one accidentally adds a height-in-meters number to a weight-in-grams.</p> <p>Yes, I know, this example is simplistic and glosses over many problems. I'd want to specify numeric values. I'd want to loop through ranges of values. I'd want to subtract two <strong>XCoord</strong> values (and get an <strong>int</strong>?). I'd want to use them as array subscripts. I'm sure there's a whole host of usages I haven't thought about in detail--and while it would be nice to have them all work transparently, I'm not averse to a little syntactic inconvenience (like a member function invocation) in some usages.</p> <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> <p>And, of course, they're just basic types with added compile-time checking--so the solution should have zero run-time performance penalties relative to the basic types, just like <strong>typedef</strong></p> <p>Does such a thing exist? Is a solution using language contructs so mind-numbingly complex and full of surprises that I would be better off using plain old <strong>typedefs</strong> and a <strong>lint</strong>-like tool instead? And if so, is there such a tool?</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