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,411 of 33,346   
   =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to Marc   
   Re: ABI of passing empty classes by valu   
   25 Aug 11 06:58:42   
   
   From: daniel.kruegler@googlemail.com   
      
   On 2011-08-25 01:44, Marc wrote:   
   > In C++, it is fairly common to pass functions an extra argument that   
   > is used purely for dispatching purposes (or at least type   
   > information), from a tag class that doesn't have any content. For   
   > instance:   
   > template  void f(Iterator i){   
   >    f(i, std::iterator_traits::iterator_category());   
   > }   
   > followed by overloads:   
   > template  void f(Iterator i,std::input_iterator_tag){   
   >    /* ... */   
   > }   
   > template  void f(Iterator i,std::forward_iterator_tag){   
   >    /* ... */   
   > }   
   > etc.   
   >   
   > In simple cases, everything is inlined and the use of the tag class is   
   > indeed pure syntactic sugar. In other examples, it isn't always   
   > inlined, and real function calls happen.   
   >   
   > Now in all ABIs I have seen, since sizeof(Tag)==1, an object of size 1   
   > is copied, put on the stack (or in a register), etc, and nothing takes   
   > advantage of the fact that it could be implemented as a function with   
   > 1 fewer argument (if it is passed by reference, that can't be ignored   
   > as someone might look at the address). Sadly being an empty class is   
   > not enough, there should also not be any user-specified copy   
   > constructor or the change becomes noticable.   
   >   
   > Would such an optimization be legal/possible? Does any compiler do it?   
   > If not, is it just considered not worth the trouble? (I admit it   
   > sounded more convincing before I thought of the copy constructor   
   > issue)   
      
   It should be possible unless you could observe the difference as described by   
   the definition of observable behaviour in the standard. Since assembler output   
   or any binary format is unspecified, the differences within such files does   
   not belong to    
   observable behaviour. If you take a pointer to a function, the types must be   
   the right ones and there can only exist a single address of a given function,   
   including inline functions.   
      
   I don't understand what your reference to the copy constructor is supposed to   
   mean. If you have the funny idea that you could define a non-trivial   
   copy-constructor for this empty type with observable side-effects, the   
   compiler naturally cannot eliminate    
   that in your example. But why should you do that?   
      
   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