Home Dashboard Directory Help

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


 as Fixed Help for as Fixed

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
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
Sign in to post a comment.
Posted by Microsoft on 4/29/2014 at 12:24 PM
Thank you for reporting this issue. This issue has been fixed in Visual Studio 2013. You can install a trial version of Visual Studio 2013 with the fix from: http://go.microsoft.com/?linkid=9832436
Posted by Microsoft on 1/25/2012 at 2:18 PM

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.

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