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,324 of 33,346   
   Seungbeom Kim to Thiago Adams   
   Re: Getting the smallest signed type tha   
   22 May 12 11:40:43   
   
   044cebfd   
   From: musiphil@bawi.org   
      
   On 2012-05-16 10:38, Thiago Adams wrote:   
   >   
   > I think we can solve the problem having a conversion table   
   > for all integral types. The table is built considering some   
   > criteria. In your case I think int32 is fine for all cases,   
   > unless you need int64.   
   >   
   > [unsigned char, unsigned chart] -> int   
   > [unsigned short, unsigned short] -> int   
   > ....more...   
   > [unsigned long long, unsigned long long] -> long long   
      
   This is not a portable solution because you don't know whether int   
   will be sufficient for unsigned char or unsigned short; it could be   
   that sizeof(char) = sizeof(short) = sizeof(int) < sizeof(long) and   
   that you need to cast unsigned char to signed long.   
      
   This is a property of the C++ implementation, so the implementation   
   trivially knows the solution to this problem, but it's quite hard to   
   come up with a satisfying solution in a user code; it's just like   
   trying to invent int_least32_t on your own (without ).   
   I hoped there would be some help from the implementation that I didn't   
   know, but there apparently isn't.   
      
   > ----   
   >   
   > template struct SelType;   
   >   
   > template<>   
   > struct SelType {   
   >   typedef int type;   
   > };   
   >   
   > template<>   
   > struct SelType {   
   >   typedef int type;   
   > };   
   >   
   > ....more...   
   >   
   > template<>   
   > struct SelType {   
   >   typedef long long type;   
   > };   
   >   
   > template   
   > void Diff(U a, U b)   
   > {   
   >     typedef SelType::type S;   
   >     S d = static_cast(a) - static_cast(b);   
   > }   
      
   If you're always using the same argument for the two template parameters   
   for SelType, why do you define SelType so that it takes two parameters?   
      
   --   
   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