3636

inline virtual method in template class

Question:

I have a template base class with a get_p_pow method that is called by a foo function:

template <typename T_container> class base { public: int foo() { ... get_p_pow(p_pow, delta_p); ... } ... protected: virtual T_container& get_p_pow(T_container &p_pow, double delta_p) const { p_pow(0) = 1.0; p_pow(1) = delta_p; for (difference_type i = 2; i <= order; ++i) { p_pow(i) *= p_pow(i-1)*delta_p; } return p_pow; } int order; };

For some derived classes, the value of order is set to a specific number, so I can unroll the loop, with the hope that foo calls and inlines the unrolled version:

template <typename T_container> class child : public base<T_container> { ... protected: T_container& get_p_pow(T_container &p_pow, double delta_p) const { p_pow(0) = 1.0; p_pow(1) = delta_p; p_pow(2) = p_pow(1)*delta_p; p_pow(3) = p_pow(2)*delta_p; p_pow(4) = p_pow(3)*delta_p; p_pow(5) = p_pow(4)*delta_p; return p_pow; } // order set to 5 in constructor };

The problem is, is that I know for virtual functions, most of the time they cannot be inlined, unless the compiler has the specific instance of the object, and not a pointer/reference to it. However, since base and child are template functions, they are located in a header file which is included with every translation unit that uses these classes. That means the compiler should know everything it needs in order to support inlining (to my knowledge, since it does not need separate compilation). I've tried this out, and basically the function isn't inlined, and it doesn't lead to any real performance benefit (in addition to function call overhead, I think pipelining gets ruined too). Is there a way to support inlining for this situation? Or is there any advice to implement this kind of thing?

Answer1:

In case of virtual methods inlining makes no real sense (as you would need runtime information do decide which code to use for inlining), so compilers generate "normal" methods out of such code, and call them "regularly".

Recommend

  • + operator on string and int
  • error: call of overloaded distance is ambiguous
  • Why are some debug symbols missing and how to track them?
  • Copying strided data in C++
  • Split dictionary of lists into single lists
  • Spock @Unroll annotation
  • Renaming class causes IBOutlet connection to fail
  • How do i do the following curl command in Java
  • Why use database factory in asp.net mvc?
  • Can someone please explain to me in the most layman terms how to use EventArgs?
  • Is there a javascript serializer for JSON.Net?
  • Deserializing XML into class C#
  • Where to put my custom functions in Wordpress?
  • Statically linking a C++ library to a C# process using CLI or any other way
  • How to limit post in wp_query
  • Upload files with Ajax and Jquery
  • Build own AppleScript numerical error handling
  • Matrix multiplication with MKL
  • Buffer size for converting unsigned long to string
  • AngularJs get employee from factory
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • python regex in pyparsing
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • How to set the response of a form post action to a iframe source?
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • Java static initializers and reflection
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • Change div Background jquery
  • need help with bizarre java.net.HttpURLConnection behavior
  • Qt: Run a script BEFORE make
  • Bitwise OR returns boolean when one of operands is nil
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships
  • reshape alternating columns in less time and using less memory
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • Converting MP3 duration time
  • How to push additional view controllers onto NavigationController but keep the TabBar?