Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomatically Instantiating over a bunch of types in C++
    primarykey
    data
    text
    <p>In our library we have a number of "plugins", which are implemented in their own cpp files. Each plugin defines a template function, and should instantiate this function over a whole bunch of types. The number of types can be quite large, 30-100 of them, and can change depending on some compile time options. Each instance really have to be compiled and optimized individually, the performance improves by 10-100 times. The question is what is the best way to instantiate all of these functions. </p> <p>Each plugin is written by a scientist who does not really know C++, so the code inside each plugin must be hidden inside macros or some simple construct. I have a half-baked solution based on a "database" of instances:</p> <pre><code>template&lt;int plugin_id, class T&gt; struct S { typedef T (*ftype)(T); ftype fp; }; // By default we don't have any instances template&lt;int plugin_id, class T&gt; S::ftype S::fp = 0; </code></pre> <p>Now a user that wants to use a plugin can check the value of</p> <pre><code>S&lt;SOME_PLUGIN,double&gt;::fp </code></pre> <p>to see if there is a version of this plugin for the double type. The template instantiation of fp will generate a weak reference, so the linker will use the "real" instance if we define it in a plugin implementation file. Inside the implementation of SOME_PLUGIN we will have an instantiation</p> <pre><code>template&lt;&gt; S&lt;SOME_PLUGIN,double&gt;::ftype S&lt;SOME_PLUGIN,double&gt;::fp = some_plugin_implementation; </code></pre> <p>This seems to work. The question is if there is some way to automatically repeat this last statement for all types of interest. The types can be stored in a template class or generated by a template loop. I would prefer something that can be hidden by a macro. Of course this can be solved by an external code generator, but it's hard to do this portably and it interfers with the build systems of the people that use the library. Putting all the plugins in header files solves the problem, but makes the compiler explode (needing many gigabytes of memory and a very long compilation time). </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload