Identical template for many functions <h1>Hello guys (and happy new year!)</h1> <p>I'm writing a (not really) simple project in C++ (my first, coming from plain C). I was wondering if there is a way to simplify the definition for multiple functions having the same template pattern. I think an example would be better to explain the problem.</p> <h2>The context</h2> <p>Let's assume I have a "Set" class which represents a list of numbers, defined as</p> <pre><code>template &lt;class T&gt; class Set { static_assert(std::is_arithmetic&lt;T&gt;(), "Template argument must be an arithmetic type."); T *_address; ... } </code></pre> <p>So if I have an instance (say <code>Set&lt;double&gt;</code>) and an array <code>U array[N]</code>, where <code>U</code> is another arithmetic type and <code>N</code> is an integer, I would like to be able to perform some operations such as assigning the values of the array to those of the <code>Set</code>. Thus I create the function template inside the class</p> <pre><code>template &lt;class U, int N&gt; void assign(U (&amp;s)[N]) { static_assert(std::is_arithmetic&lt;U&gt;(), "Template argument must be an arithmetic type."); errlog(E_BAD_ARRAY_SIZE, N == _size); idx_t i = 0; do { _address[i] = value[i]; } while (++i &lt; size); } </code></pre> <h2>The problem</h2> <p>As far as my tests went the code above works perfectly fine. However I find it REALLY REALLY ugly to see since I need the <code>static_assert</code> to ensure that only arithmetic types are taken as arguments (parameter <code>U</code>) and I need a way to be sure about the array size (parameter <code>N</code>). Also, I'm not done with the <code>assign</code> function but I need so many other functions such as <code>add</code>, <code>multiply</code>, <code>scalar_product</code> etc. etc.!</p> <h2>The first solution</h2> <p>I was wondering then if there is a prettier way to write this kind of class. After some work I've come up with a preprocessor directive:</p> <pre><code>#define arithmetic_v(_U_, _N_, _DECL_, ...) \ template &lt;class U, idx_t N&gt; _DECL_ \ { \ static_assert(std::is_arithmetic&lt;U&gt;(),"Rvalue is not an arithmetic type."); \ errlog(E_BAD_ARRAY_SIZE, N == _size); \ __VA_ARGS__ \ } </code></pre> <p>thus defining my function as</p> <pre><code>arithmetic_v(U, N, void assign(U (&amp;value)[N]), idx_t i = 0; do { _address[i] = value[i]; } while (++i &lt; _size); ) </code></pre> <p>This is somehow cleaner but still isn't the best since I'm forced to lose the brackets wrapping the function's body (having to include the <code>static_assert</code> INSIDE the function itself for the template parameter <code>U</code> to be in scope).</p> <h2>The question</h2> <p>The solution I've found seems to work pretty well and the code is much more readable than before, but... Can't I use another construct allowing me to build an even cleaner definition of all the functions and still preserving the <code>static_assert</code> piece and the info about the array size? It would be really ugly to repeat the template code once for each function I need...</p> <h2>The thanks</h2> <p>I'm just trying to learn about the language, thus ANY additional information about this argument will be really appreciated. I've searched as much as I could stand but couldn't find anything (maybe I just couldn't think of the appropriate keywords to ask Google in order to find something relevant). Thanks in advance for your help, and <strong>happy new year to you all</strong>!</p> <p>Gianluca</p>