<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - clang++ fails to optimize variadic template constructor with fold expression"
   href="https://bugs.llvm.org/show_bug.cgi?id=37449">37449</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>clang++ fails to optimize variadic template constructor with fold expression
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>vittorio.romeo@outlook.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider the following wrapper around `std::vector`:

    #include <vector>

    struct init { };

    template <typename T>
    struct vec : std::vector<T>
    {
        using std::vector<T>::vector;

        template <typename... Xs>
        vec(init, Xs&&... xs) 
        {
            this->reserve(sizeof...(Xs));
            (this->emplace_back(std::forward<Xs>(xs)), ...);
        }
    };

When compiling the following code `clang++-trunk -Ofast -std=c++2a`...

    void foo() 
    {
    #ifndef VARIADIC
        vec<std::string> v;
        v.reserve(2);
        v.emplace_back("hello");
        v.emplace_back("world");
    #else
        vec<std::string> v{init{}, "hello", "world"};
    #endif
    }

...the variadic version produces roughly 5x more assembly instructions and is
around 46% slower at run-time.

Marking `vec`'s constructor as `__attribute__((always_inline))` makes both
versions produce the same assembly.

Live assembly comparison on godbolt.org:
<a href="https://godbolt.org/g/YwM86C">https://godbolt.org/g/YwM86C</a>

Live run-time benchmark on quick-bench.com:
<a href="http://quick-bench.com/C5_4Bm5Crsqg1ykE0YmoHFOwmLs">http://quick-bench.com/C5_4Bm5Crsqg1ykE0YmoHFOwmLs</a>

I think that this is a very reasonable/common usage of a variadic template
constructor + fold expression - it is surprising to me as a developer that
clang++ doesn't optimize this. This *should* be a better alternative in terms
of run-time performance to `std::initalizer_list`, which prevents moves and has
other significant drawbacks - it is disappointing to see `always_inline` being
required here.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>