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 31,564 of 33,346   
   =?windows-1252?Q?Daniel_Kr=FCgler?= to All   
   Re: istream_iterator as argument : why e   
   13 Oct 11 16:47:02   
   
   c1a5726e   
   From: daniel.kruegler@googlemail.com   
      
   Am 13.10.2011 23:13, schrieb ptyxs:   
   > A probably silly question of mine...   
   >   
   > Reading the famous book by Josuttis "The C++ Standard Library", I am   
   > wondering why, in the program given Chapter 6, section 9, page 228,   
   > the external parentheses for the first argument in the set constructor   
   > are needed.   
   > Here is it :   
   >   
   > #include   
   > #include   
   > #include   
   > #include   
   > #include   
   > using namespace std;   
   >   
   > int main()   
   > {   
   >     set  coll((istream_iterator(cin)),   
   > istream_iterator());   
   >     copy(coll.begin(),coll.end(),ostream_iterator(cout,"\n"));   
   > }   
      
   Yes, this is necessary in C++03, but it can be simpler expressed in   
   C++11 by writing it as:   
      
   #include    
   #include    
   #include    
   #include    
   #include    
   using namespace std;   
      
   int main()   
   {   
      set coll(istream_iterator(cin),   
   istream_iterator{});   
      copy(coll.begin(),coll.end(),ostream_iterator(cout,"\n"));   
   }   
      
   (Note the pair of braces in the second argument to construct coll)   
      
   > Why not simply :   
   >   
   > #include   
   > #include   
   > #include   
   > #include   
   > #include   
   > using namespace std;   
   >   
   > int main()   
   > {   
   >     set  coll(istream_iterator(cin),   
   > istream_iterator());   
   >     copy(coll.begin(),coll.end(),ostream_iterator(cout,"\n"));   
   > }   
      
   Yes, this is the classic "most vexing parse" of C++. The reason for this   
   code being ill-formed is due to the fact, that the compiler is required   
   to interpret   
      
   set  coll(istream_iterator(cin),   
   istream_iterator());   
      
   as a *function* declaration: The name of the function is coll, the   
   return type is std::set, the first parameter type is   
   std::istream_iterator (The first parameter name is cin),   
   the second parameter type is   
   std::istream_iterator>(*)().   
      
   The last interpretation is another surprise, because a function type -   
   istream_iterator() is a function type returning   
   istream_iterator without any parameter types - is interpreted as   
   a function pointer if given as function parameter. This rule is similar   
   to another one, where an array type provided as function parameter   
   actually represents a parameter of pointer type. E.g.   
      
   void foo(int a[2]);   
      
   is valid but is equivalent to   
      
   void foo(int* a);   
      
   These are one of the ugliest aspects of C++. Fortunately the new   
   extended use cases for braces (as shown above) will often help resolving   
   such unwanted interpretations.   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
   --   
         [ 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