Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Perhaps something like:</p> <pre><code>template&lt;typename T&gt; struct register_struct { virtual void operator()() = 0; register_struct(); register_struct *pNext; }; template&lt;typename T&gt; struct registry { static register_struct&lt;T&gt; *chain; static void walk() { register_struct&lt;T&gt; *p = chain; while (p) { (*p)(); p = p-&gt;pNext; } } }; template&lt;typename T&gt; register_struct&lt;T&gt; *registry&lt;T&gt;::chain = NULL; template&lt;typename T&gt; register_struct&lt;T&gt;::register_struct() { pNext = registry&lt;T&gt;::chain; registry&lt;T&gt;::chain = this; } #define DECL_REGISTRY(name) \ struct tag_##name { } ; \ void name() { registry&lt;tag_##name&gt;::walk(); } #define JOIN_EXPAND(x, y) JOIN_EXPAND_2(x, y) #define JOIN_EXPAND_2(x, y) x ## y // Invoke REGISTER_PRINT at file scope! #define REGISTER_PRINT(name, text) \ namespace { \ static struct : public register_struct&lt;tag_##name&gt; { \ void operator()() { \ std::cout &lt;&lt; text &lt;&lt; std::endl; \ } \ } JOIN_EXPAND(rs_##name##_, __LINE__); \ } DECL_REGISTRY(foo); REGISTER_PRINT(foo, "hello") REGISTER_PRINT(foo, "world") int main() { foo(); return 0; } </code></pre> <p>This will fix the recursive instantiation issues, but you're still limited to one registration per line. However, since the registrations are at file scope, this should (hopefully!) be less of a problem.</p> <p>Note that this is unsafe to use if you expect to invoke these prior to <code>main()</code> - you must allow static constructors to complete before using it.</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.
    1. VO
      singulars
      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