C++ parameter pack, constrained to have instances of a single type?
<p>Since C++11 we can make template functions which can accept any sequence of arguments:</p>

<pre><code>template &lt;typename... Ts&gt;
void func(Ts &amp;&amp;... ts) {
   step_one(std::forward&lt;Ts&gt;(ts)...);
   step_two(std::forward&lt;Ts&gt;(ts)...);
}
</code></pre>

<p>However, suppose that it really only makes sense to call my function in the case where each argument has the same type -- any number of arguments would be okay though.</p>

<p>What's the best way to do that, i.e. is there a good way to constrain the templates to make a nice error message in that case, or ideally, eliminate <code>func</code> from participating in overload resolution when the arguments don't match?</p>

<hr>

<p>I can make it really concrete if it helps:</p>

<p>Suppose I have some struct:</p>

<pre><code>struct my_struct {
  int foo;
  double bar;
  std::string baz;
};
</code></pre>

<p>Now, I want to be able to do things like, print the members of the struct for debugging purposes, serialize and deserialize the struct, visit the members of the struct in sequence, etc. I have some code to help with that:</p>

<pre><code>template &lt;typename V&gt;
void apply_visitor(V &amp;&amp; v, my_struct &amp; s) {
  std::forward&lt;V&gt;(v)("foo", s.foo);
  std::forward&lt;V&gt;(v)("bar", s.bar);
  std::forward&lt;V&gt;(v)("baz", s.baz);
}

template &lt;typename V&gt;
void apply_visitor(V &amp;&amp; v, const my_struct &amp; s) {
  std::forward&lt;V&gt;(v)("foo", s.foo);
  std::forward&lt;V&gt;(v)("bar", s.bar);
  std::forward&lt;V&gt;(v)("baz", s.baz);
}

template &lt;typename V&gt;
void apply_visitor(V &amp;&amp; v, my_struct &amp;&amp; s) {
  std::forward&lt;V&gt;(v)("foo", std::move(s).foo);
  std::forward&lt;V&gt;(v)("bar", std::move(s).bar);
  std::forward&lt;V&gt;(v)("baz", std::move(s).baz);
}
</code></pre>

<p>(It looks a bit laborious to generate code like this, but I made <a href="https://github.com/cbeck88/visit_struct" rel="noreferrer">a small library</a> some time ago to help with that.)</p>

<p>So, now I would like to extend it so that it can visit two instances of <code>my_struct</code> at the same time. The use of that is, what if I want to implement equality or comparison operations. In <code>boost::variant</code> documentation they call that "binary visitation" as contrasted with "unary visitation".</p>

<p>Probably, no one will want to do more than binary visitation. But suppose I want to do like, general <code>n-ary</code> visitation. Then, it looks like this I guess</p>

<pre><code>template &lt;typename V, typename ... Ss&gt;
void apply_visitor(V &amp;&amp; v, Ss &amp;&amp; ... ss) {
  std::forward&lt;V&gt;(v)("foo", (std::forward&lt;Ss&gt;(ss).foo)...);
  std::forward&lt;V&gt;(v)("bar", (std::forward&lt;Ss&gt;(ss).bar)...);
  std::forward&lt;V&gt;(v)("baz", (std::forward&lt;Ss&gt;(ss).baz)...);
}
</code></pre>

<p>But now, it's getting a little more squirrelly -- if someone passes a series of types that aren't even the same structure type at all, the code may still compile and do something totally unexpected by the user.</p>

<p>I thought about doing it like this:</p>

<pre><code>template &lt;typename V, typename ... Ss&gt;
void apply_visitor(V &amp;&amp; v, Ss &amp;&amp; ... ss) {
  auto foo_ptr = &amp;my_struct::foo;
  std::forward&lt;V&gt;(v)("foo", (std::forward&lt;Ss&gt;(ss).*foo_ptr)...);
  auto bar_ptr = &amp;my_struct::bar;
  std::forward&lt;V&gt;(v)("bar", (std::forward&lt;Ss&gt;(ss).*bar_ptr)...);
  auto baz_ptr = &amp;my_struct::baz;
  std::forward&lt;V&gt;(v)("baz", (std::forward&lt;Ss&gt;(ss).*baz_ptr)...);
}
</code></pre>

<p>That at least will cause a compile error if they use it with mismatching types. But, it's also happening too late -- it's happening after the template types are resolved, and after overload resolution I guess.</p>

<p>I thought about using SFINAE, like, instead of returning void, using <code>std::enable_if_t</code> and checking some expression <code>std::is_same&lt;std::remove_cv_t&lt;std::remove_reference_t&lt;...&gt;&gt;</code> for each type in the parameter pack.</p>

<p>But for one, that SFINAE expression is pretty complicated, and for two, it also has a drawback -- suppose someone has a derived class <code>struct my_other_struct : my_struct { ... }</code>, and they want to use it with the visitor mechanism, so some of the parameters are <code>my_struct</code> and some are <code>my_other_struct</code>. Ideally the system would convert all the references to <code>my_struct</code> and apply the visitor that way, and afaik the example I gave above with the member pointers <code>foo_ptr</code>, <code>bar_ptr</code>, <code>baz_ptr</code> would do the right thing there, but it's not even clear to me how to write a constraint like that with SFINAE -- I would have to try to find a common base of all the parameters I guess?</p>

<p>Is there a good way to reconcile those concerns in general?</p>