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,396 of 33,346   
   Marc to All   
   ABI of passing empty classes by value   
   24 Aug 11 16:44:20   
   
   From: marc.glisse@gmail.com   
      
   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)   
      
      
   --   
         [ 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