<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 - RVO fails in template function with 'auto' return type"
   href="https://bugs.llvm.org/show_bug.cgi?id=39326">39326</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>RVO fails in template function with 'auto' return type
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>7.0
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>All
          </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>C++14
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>wenzel.jakob@epfl.ch
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>I've run into the following rather bizarre bug on Clang that appears to be a
standard violation: when a function template has an 'auto' return value, return
value optimization (RVO) no longer works. Everything is okay for non-template
functions and functions with an explicitly specified return value type.

Consider the following piece of code that reproduces this issue:

// ========================================================

#include <stdio.h>

struct A {
    A() { printf("A::A()\n"); }
    A(const A&) { printf("A::A(const A&)\n"); }
    A(A&&) { printf("A::A(A&&)\n"); }
    ~A() { printf("A::~A()\n"); }
    A& operator=(const A&) { printf("A::operator=(const A&)\n"); return *this;
}
    A& operator=(A&&) { printf("A::operator=(A&&)\n"); return *this;}
};

A test1(int z) { A x; return x; }

auto test2(int z) { A x; return x; }

template <typename T> A test3(T z) { A x; return x; }

template <typename T> auto test4(T z) { A x; return x; } /// <- problem!

int main(int argc, char *argv[]) {
    printf("\n-- Test 1:\n");
    test1(1);
    printf("\n-- Test 2:\n");
    test2(1);
    printf("\n-- Test 3:\n");
    test3(1);
    printf("\n-- Test 4:\n");
    test4(1);
    return 0;
}

// ========================================================

This produces the following output:

$ clang++ test.cpp -o test -std=c++14
$ ./test
-- Test 1:
A::A()
A::~A()

-- Test 2:
A::A()
A::~A()

-- Test 3:
A::A()
A::~A()

-- Test 4:
A::A()
A::A(A&&)
A::~A()
A::~A()


Comparison (GCC 8):

-- Test 1:
A::A()
A::~A()

-- Test 2:
A::A()
A::~A()

-- Test 3:
A::A()
A::~A()

-- Test 4:
A::A()
A::~A()</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>