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,554 of 33,346   
   fmatthew5876 to All   
   Template typedef and ternary operator.   
   23 Sep 12 21:25:43   
   
   From: fmatthew5876@googlemail.com   
      
   I want to implement an optimization at compile time based on whether   
   the size of a pointer is large enough to store an object. Without   
   trying to explain it, I'll just show some code.   
      
   class FastBar {   
     public:   
      void* store_id(uint64_t id) {   
        return reinterpret_cast(id);   
      }   
      uint64_t get_id(void* v) {   
        return reinterpret_cast(v);   
      }   
   };   
      
   class SlowBar {   
     public:   
      void* store_id(uint64_t id)   
      {   
       _vec.push_back(id);   
       return reinterpret_cast(   
          (uintptr_t)_vec.size());   
      }   
      uint64_t get_id(void* v) {   
       return _vec[reinterpret_cast(v)];   
      }   
     private:   
      std::vector _vec;   
   };   
      
   template    
   class _Foo : public BAR {   
     //some other stuff   
   };   
      
   typedef _Foo<   
   sizeof(void*) < sizeof(uint64_t)   
   ? SlowBar   
   : FastBar> Foo;   
      
   The compiler complains about the ternary operator in the typedef. I   
   want to be able to determine which Bar baseclass to use at compile   
   time and then use Foo in the rest of my code and not have to worry   
   about which Bar is used underneath.   
      
   Basically all I'm trying to accomplish is a compile time optimization   
   if pointers are large enough to directly store 64 bit integers. If   
   they aren't then they store an index into some temporary storage that   
   holds the integers. I'd be happy to just use the preprocessor but I'm   
   not aware of any simple cross platform way to determine the size of   
   pointers at that level.   
      
   How would you implement something like this?   
      
      
      
   --   
         [ 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