Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 33,322 of 33,346    |
|    Peter Chapin to Gonzalo BG    |
|    Re: Class to behave like ints    |
|    09 May 14 06:10:01    |
      From: PChapin@vtc.vsc.edu              On 2014-05-08 08:46, Gonzalo BG wrote:              > But in summary, C++ is a very bad language for this _right now_. Since       > you cannot write an user-defined integer class models an Integral       > concept and use it with the rest of the language and standard library.       >       > In Haskell this all works flawlessly since: newtype allows you to easily       > create new types from existing ones, and typeclasses (concepts) are used       > everywhere.              At the risk of being off topic I might also mention that Ada supports       this ability very well. What the OP is trying to do is trivial:              type Apple_ID is new Integer;       type Pear_ID is new Integer;              Now objects of type Apple_ID and Pear_ID can't be mixed (or mixed with       integers) yet they inherit all the values and operations of Integer.       However it is true that to support this there are pervasive effects on       the language. For example indexing an array by an Apple_ID is a type       error unless you define an array type specifically that can accept such       an index              type Flavor_Array_Type is array(Apple_ID range 1 .. 10) of Flavor;              An object of type Flavor_Array_Type can only be indexed by Apple_ID (and       not integers), etc.              The Ada community regards this as a strong feature because it allows you       to create scalar types for logically distinct concepts and let the type       system catch errors involving them... such as mixing an apple counter       value with a buffer offset, etc.              I've often thought that creative use of C++ templates could probably       realize many of the same goals but it would be a rather heavy handed way       of emulating what, at its heart, is a simple concept. Also, as was       pointed out, since the C++ language and library does not "think" this       way, I expect aggressive use of distinct scalar types would be an uphill       battle in typical C++ code bases.                     --        [ See http://www.gotw.ca/resources/clcm.htm for info about ]        [ comp.lang.c++.moderated. First time posters: Do this! ]              --- SoupGate-Win32 v1.05        * Origin: you cannot sedate... all the things you hate (1:229/2)    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca