Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The only purpose of <code>foo</code> in the OP is triggering the check when it's instantiated. That's why you need the variable <code>hello</code>: it's an instantiation of <code>foo</code>.</p> <p>I would rather follow the approach of traits in <code>&lt;type_traits&gt;</code>. More precisely, I would turn <code>perform_checks</code> into <code>class</code> (or <code>struct</code>) that has public <code>static constexpt bool</code> member called <code>value</code> which is <code>true</code> or <code>false</code> depending on whether the given types pass the test or not. Then I would use a single <code>static_assert</code> to stop compilation if <code>value</code> is false.</p> <p>My solution, which assumes that the number of template type arguments is even, follows:</p> <pre><code>#include &lt;type_traits&gt; template&lt;typename First, typename Second, typename... Others&gt; struct perform_checks : std::integral_constant&lt;bool, perform_checks&lt;First, Second&gt;::value &amp;&amp; // Checks First and Second perform_checks&lt;Others...&gt;::value // Recursively "calls" itself on Others &gt; { }; // This specialization finishes the recursion and effectively performs the test template&lt;typename First, typename Second&gt; struct perform_checks&lt;First, Second&gt; : std::integral_constant&lt;bool, std::is_default_constructible&lt;First&gt;::value &amp;&amp; // Checks First std::is_copy_constructible&lt;Second&gt;::value // Checks Second &gt; { }; </code></pre> <p>Here is a simple test:</p> <pre><code>struct NonDefaultConstructible { NonDefaultConstructible() = delete; }; struct NonCopyConstructible { NonCopyConstructible(const NonCopyConstructible&amp;) = delete; }; int main() { static_assert(perform_checks&lt;int, double&gt;::value, "Failure"); static_assert(perform_checks&lt;int, int, double, double&gt;::value, "Failure"); static_assert(!perform_checks&lt;NonDefaultConstructible, int&gt;::value, "Failure"); static_assert(!perform_checks&lt;int, NonCopyConstructible&gt;::value, "Failure"); static_assert(!perform_checks&lt;int, int, double, NonCopyConstructible&gt;::value, "Failure"); } </code></pre> <p>Notice that no variable was created.</p>
 

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