Search

C++ compiler - decltype doesn't infer the correct return type - vector<int>& instead of vector<int>. by lucabol

Closed
as Fixed Help for as Fixed

1
0
Sign in
to vote
Type: Bug
ID: 719083
Opened: 1/18/2012 1:10:25 AM
Access Restriction: Public
Moderator Decision: Sent to Engineering Team for consideration
3
Workaround(s)
0
User(s) can reproduce this bug
decltype doesn't infer the correct return type - vector<int>& instead of vector<int>. That causes the code below to give the wrong result.

BTW: Bcc appears to produce my expected result
BTW2: let me know if there is a workaround to help the compiler inference
Details (expand)

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

Visual Studio 2010

Steps to reproduce

1. copy and paste the code below in an empty console project
2. run it

#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>

using namespace std;

template <class A, class F1>
inline auto pipe(const A& a, const F1& f1) -> decltype(f1(a)) { return f1(a); }

vector<int> less_than_5(const vector<int>& v) {
     vector<int> v1;
     remove_copy_if(v.begin(), v.end(), back_inserter(v1), [](int i) {return i >= 5;});
     return v1;
}

int main()
{
    int nums[] = {1,2,3,4,5,6,7,8,9,10};
    vector<int> v (&nums[0], &nums[9]);


    auto k = pipe(v, less_than_5);

    cout << "k shouldn't be empty, but it is: "<< k.empty() << endl;

    return 0;
}

Product Language

English

Operating System

Windows 7

Operating System Language

English

Actual results

The following string is printed:

k shouldn't be empty, but it is: 1

Expected results

The following string is printed:

k shouldn't be empty, but it is: 0
File Attachments
0 attachments
Sign in to post a comment.
Posted by Microsoft on 1/25/2012 at 2:18 PM
Hello,

Thank you for reporting this. It indeed was a bug and we've fixed it in the current in-progress development cycle. You will see it fixed in the next major release of VS.

Thanks,
Ulzii Luvsanbat
Windows C++ Team
Posted by MS-Moderator10 [Feedback Moderator] on 1/18/2012 at 6:10 PM
Thank you for submitting feedback on Visual Studio 2010 and .NET Framework. Your issue has been routed to the appropriate VS development team for investigation. We will contact you if we require any additional information.
Posted by MS-Moderator01 on 1/18/2012 at 1:43 AM
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.
Posted by lucabol on 1/26/2012 at 12:59 AM
Thanks. The workaround by Viorel_ works nicely.
Posted by aljodAv on 1/18/2012 at 2:01 PM
Make less_than_5(...) return a reference variable, and make it contain a static local variable whose value is returned:

#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>

using namespace std;

template <class A, class F1>
auto pipe(const A& a, const F1& f1) -> decltype(f1(a)) { return f1(a); }

vector<int>/*added*/& less_than_5(const vector<int>& v) {
    /*added*/static vector<int> v1;
    remove_copy_if(v.begin(), v.end(), back_inserter(v1), [](int i) {return i >= 5;});
    return v1;
}

int main()
{
    int nums[] = {1,2,3,4,5,6,7,8,9,10};
    vector<int> v (&nums[0], &nums[9]);


    auto k = pipe(v, less_than_5);

    cout << "k shouldn't be empty, but it is: "<< k.empty() << endl;

    return 0;
}
Posted by Viorel_ on 1/18/2012 at 7:06 AM
Try this workaround:


template< class A, class F1 >
inline auto pipe(const A & a, const F1 & f1) ->
    decltype( std::remove_reference< decltype(f1(a)) >::type() )
{
    return f1(a);
}