Performance hit of vtable lookup in C++

前端 未结 6 1185
日久生厌
日久生厌 2020-12-24 03:52

I\'m evaluating to rewrite a piece of real-time software from C/assembly language to C++/assembly language (for reasons not relevant to the question parts of the code are ab

6条回答
  •  一整个雨季
    2020-12-24 04:26

    You can hide the void* type erasure and type recovery inside templates. The result would (hopefully) be the same array to function pointers. This yould help with casting and compatible to your code:

    #include 
    
    template
    void fun(void* param) {
      F f;
      f(*static_cast(param));
    }
    
    struct my_function {
      void operator()(int& i) {
        std::cout << "got it " << i << std::endl;
      }
    };
    
    
    int main() {
      void (*func)(void*) = fun;
    
      int j=4;
      func(&j);
    
      return 0;
    }
    

    In this case you can create new functions as a function object with more type safty. The "normal" OOP aproach with virtual functions doesn't help here.

    In case of A C++11 environment you could create the array with help of variadic templates at compile time (but with an complicated syntax).

提交回复
热议问题