Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm using C++11 to outline you a solution (the code is tested, too), but this is doable in C++03 (using e.g. Boost.Tuple):</p> <pre><code>// Base case template&lt;typename Lhs, typename Rhs&gt; std::tuple&lt;Lhs&amp;, Rhs&amp;&gt; operator,(Lhs&amp; lhs, Rhs&amp; rhs) { return std::tie(lhs, rhs); } // General case when we already have a tuple template&lt;typename... Lhs, typename Rhs&gt; std::tuple&lt;Lhs..., Rhs&amp;&gt; operator,(std::tuple&lt;Lhs...&gt;&amp;&amp; lhs, Rhs&amp; rhs) { return std::tuple_cat(lhs, std::tie(rhs)); } </code></pre> <p>Usage looks like (assuming this operator resides in <code>namespace ns</code>):</p> <pre><code>// Declaration: note how ff must return a tuple std::tuple&lt;X, Y, Z&gt; ff(A, B, C); A a = /* ... */; B b = /* ... */; C c = /* ... */; X x; Y y; Z z; using ns::operator,; // brackets on the left-hand side are required (x, y, z) = ff(a, b, c); </code></pre> <p>Here are the attached caveats:</p> <ul> <li><p>as you've seen, you need a using declaration to bring <code>operator,</code> in scope. Even if the types <code>X</code>, <code>Y</code>, <code>Z</code> reside in the same scope of <code>operator,</code> (to enable ADL), <code>std::tuple</code> doesn't. You <em>could</em> do something like <code>template&lt;typename... T&gt; struct tuple: std::tuple&lt;T...&gt; { using std::tuple&lt;T...&gt;::tuple; };</code> (not as convenient to do in C++03 however) to have your own tuple in the appropriate namespace to have ADL in a quick-and-dirty way. However:</p></li> <li><p>overloaded operators must always operate on at least one user-defined type. So if types <code>X</code> and <code>Y</code> both happen to be types like <code>int</code> or <code>double</code>, then you get the default <code>operator,</code>. The usual solution to things like this is to require the client to do instead something like <code>(ref(x), ref(y), z) = ff(a, b, c);</code> where <code>ref</code> will return a type in the appropriate namespace (for ADL purposes, again). Perhaps such type can be implemented in terms of <code>std::reference_wrapper</code> (or the Boost version, for C++03) with the same quick-and-dirty hack as for the tuple. (You'd need additional overloads of <code>operator,</code>.)</p></li> </ul> <p>All in all, that's a lot of work (with ugly workarounds) when something like</p> <pre><code>/* declaration of ff and variable definitions the same as before */ std::tie(x, y, z) = ff(a, b, c); </code></pre> <p>or perhaps</p> <pre><code>/* this skips unnecessary default constructions of x, y, z */ auto tuple = ff(a, b, c); using std::get; auto&amp; x = get&lt;0&gt;(tuple); auto&amp; y = get&lt;1&gt;(tuple); auto&amp; z = get&lt;2&gt;(tuple); </code></pre> <p>works out of the box (even in C++03 with Boost.Tuple). My advice in this matter would be (with no slight intended): <strong>Keep it simple, stupid!</strong> and to use that.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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