Search

Inconsistent behavior when passing rvalue reference to function vs. function pointer by darius256

Closed
as Fixed Help for as Fixed

1
1
Sign in
to vote
Type: Bug
ID: 747514
Opened: 6/7/2012 4:37:43 PM
Access Restriction: Public
0
Workaround(s)
0
User(s) can reproduce this bug
When passing an rvalue reference to a primitive object to a function pointer, it appears that a copy of the object is made. This does not appear to occur when the function is called directly, or when the type being passed is a user-defined type.
Details (expand)

Visual Studio/Team Foundation Server/.NET Framework Tooling Version

Visual Studio 2012 RC

Steps to reproduce

The following testcase demonstrates the issue:

#include <stdio.h>
#include <functional>

struct X {};

void foo(int&& a1, void* expected)
{
    fprintf(stderr, "\t&a1 = %p [%s]\n", &a1, ((&a1 == expected) ? "true" : "false"));
}

void foo(X&& a1, void* expected)
{
    fprintf(stderr, "\t&a1 = %p [%s]\n", &a1, ((&a1 == expected) ? "true" : "false"));
}

int main(int argc, char* argv[])
{
    {
        int a1 = 1;
        fprintf(stderr, "&a1 = %p\n", &a1);
        fprintf(stderr, "primitive, direct:\n");
        foo(std::move(a1), &a1);

        fprintf(stderr, "primitive, function pointer:\n");
        typedef void(*func_ptr)(int&&, void*);
        func_ptr fp = foo;
        fp(std::move(a1), &a1);
    }

    {
        X a1;
        fprintf(stderr, "&a1 = %p\n", &a1);
        fprintf(stderr, "UDT, direct:\n");
        foo(std::move(a1), &a1);

        fprintf(stderr, "UDT, function pointer:\n");
        typedef void(*func_ptr)(X&&, void*);
        func_ptr fp = foo;
        fp(std::move(a1), &a1);
    }
}

Compile with:

cl.exe <cppfile>

Product Language

English

Operating System

Windows 7

Operating System Language

English

Actual results

&a1 = 0031F7B8
primitive, direct:
        &a1 = 0031F7B8 [true]
primitive, function pointer:
        &a1 = 0031F7B4 [false]
&a1 = 0031F7BF
UDT, direct:
        &a1 = 0031F7BF [true]
UDT, function pointer:
        &a1 = 0031F7BF [true]

Expected results

I would expect the behavior to be consistent, and that a copy would not be made regardless of whether the function is invoked directly or through a pointer.
File Attachments
0 attachments
Sign in to post a comment.
Posted by Microsoft on 11/8/2012 at 10:06 AM
Hi:
    A fix for this issue has been checked into the compiler sources. The fix should show up in the next release of Visual C++.

Xiang Fan
Visual C++ Team
Posted by Microsoft on 6/8/2012 at 6:01 AM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Microsoft on 6/7/2012 at 4:53 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.