home bbs files messages ]

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 32,328 of 33,346   
   Seungbeom Kim to Seungbeom Kim   
   Re: Getting the smallest signed type tha   
   23 May 12 12:12:51   
   
   From: musiphil@bawi.org   
      
   On 2012-05-15 21:23, Seungbeom Kim wrote:   
   >   
   > The problem is, how do you determine S? Of course, S should be large   
   > enough to represent all the values of U. To be safe, you could just   
   > use S=intmax_t for every U, but that might be an overkill; if   
   > U=uint16_t, then S=uint32_t is enough, and you don't want to use   
   > more expensive arithmetic in uint64_t or uint128_t. So, something   
   > like S=int_least[N+1]_t where N=numeric_limits ::digits() would   
   > be desirable (+1 because of the sign bit).   
      
   A useful tool for programmers to have is a template version of   
   [u]int_{least,fast}[N]_t: if we had   
      
        namespace std   
        {   
            template  struct int_least   
            {   
                typedef ... type;   // defined only if a signed integer type   
                                    // at least N bits wide exists   
            };   
      
            template  struct int_fast;    // similarly   
        }   
      
   then the original problem is solved:   
      
        typedef std::int_least::digits()+1>::type S;   
      
   And it's quite easy for implementations to provide as well, using   
   recursion and a few specializations for those values of N for which   
   the implementation has std::int_leastN_t, including 8, 16, 32 and 64.   
      
   --   
   Seungbeom Kim   
      
      
         [ 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