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,279 of 33,346   
   Alf P. Steinbach to William Lee   
   Re: Constructor-like initialization of p   
   10 May 12 21:20:30   
   
   883f14a1   
   From: alf.p.steinbach+usenet@gmail.com   
      
   On 10.05.2012 23:26, William Lee wrote:   
   >   
   > In C++ some primitives can be initialized using constructor-like   
   > syntax, like   
   >   
   >      int x = int(); // will zero-initialize x   
   >   
   > However, for primitives with multi-word types, I see different   
   > behavior depending on the compiler. For example:   
   >   
   >      long long ll = long long();   
   >      unsigned int ui = unsigned int();   
   >   
   > In 2 compilers I tried (clang 3.0 and GCC 4.7), the above two lines   
   > of code fail to compile because of the free-standing "long" and   
   > "unsigned" words, respectively. A third compiler (MSVC) compiles the   
   > code successfully.   
      
   MSVC is formally wrong but most practical, I think.   
      
      
   > I know the alternatives (like using "0" as an initialization value),   
   > but I'm curious to know what is the expected behavior for using   
   > constructor- like syntax for initialization of these primitives.   
      
   "long long" is not a proper typename. E.g. you can't do "x.~int()",   
   and likewise you can't do "x.~long long()". But with just a little   
   typedef you can do "x.~Int()" and "x.~LongLong()", with proper names.   
      
   Instead of explicit naming, with old C++03 you can always do   
      
       template< class TypeT >   
       struct Type { typedef TypeT T; };   
      
   and write   
      
       long long ll = Type::T();   
      
   In C++11 that can be expressed even more concisely by using `using`.   
      
   I'm guessing Johannes will post about that nifty trick, so I don't.   
      
   Another aspect of the problem is how to express default-initialization   
   uniformly.   
      
   For that you can simply use a wrapper,   
      
       template< class Type >   
       struct Initialized   
       {   
           Type o;   
           Initialized(): o() {}   
       };   
      
   Then you can write   
      
       Initialized ll;   
      
   As opposed to the copy initialization syntax, this should work even if   
   Type doesn't have an accessible copy constructor.   
      
   I seem to recall that Boost has a similar thing, but installing Boost   
   may possibly be overkill if this is the only thing you want from   
   Boost.   
      
   Of course, in C++11 you can use curly braces, but I don't think   
   they're supported even in Visual C++11. :-(   
      
      
   Cheers & hth.,   
      
   - Alf   
      
      
   --   
         [ 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