[llvm-bugs] [Bug 39326] New: RVO fails in template function with 'auto' return type

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Oct 16 09:04:08 PDT 2018


https://bugs.llvm.org/show_bug.cgi?id=39326

            Bug ID: 39326
           Summary: RVO fails in template function with 'auto' return type
           Product: clang
           Version: 7.0
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++14
          Assignee: unassignedclangbugs at nondot.org
          Reporter: wenzel.jakob at epfl.ch
                CC: llvm-bugs at lists.llvm.org

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()

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20181016/15134c12/attachment.html>


More information about the llvm-bugs mailing list