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

            Bug ID: 23440
           Summary: Implicitly converts lvalue to rvalue when returning
                    reference parameter in function template
           Product: clang
           Version: 3.5
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: Frontend
          Assignee: [email protected]
          Reporter: [email protected]
                CC: [email protected]
    Classification: Unclassified

#include <type_traits>

template<typename T>
auto compiles(T && t) -> T && {
    static_assert(std::is_same<T, int>::value, "Incorrect type deduced for
T.");
    return t;
}
static_assert(std::is_same<int &&, decltype(compiles(0))>::value, "shouldn't
compile");

auto fails(int && t) -> int && {
    return t;
}
static_assert(std::is_same<int &&, decltype(fails(0))>::value, "doesn't
compile");


Neither function should compile. In the function template case, T is deduced as
int, so the parameter type is int &&. Local variables and function parameters
taken by value can be implicitly moved into the return value, but not function
parameters taken by rvalue reference (t is an lvalue, even though decltype(t)
== int &&).

The non-template function correctly honors this behavior.

If the return type of compiles is changed to decltype(auto) or auto &&, then
the function correctly returns int & and the static_assert fires.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
LLVMbugs mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/llvmbugs

Reply via email to