decltype deduces wrong type - by jensa79

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


1
0
Sign in
to vote
ID 725189 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 2/16/2012 12:24:53 AM
Access Restriction Public
Moderator Decision Sent to Engineering Team for consideration

Description

#include <vector> 


struct Int 
{ 
  explicit Int(int); 



}; 


Int intfunc(Int, Int); 

template<typename FwdIt, typename Func, typename T> 
auto foldl(FwdIt first, FwdIt last, Func f, T initial) -> 
decltype(f(initial, *first)) 
{ 
  for(; first != last; ++first) 
  { 
     initial = f(initial, *first); 
  } 


  return initial; 



} 


// instantiate template to check compilation 
void instantiate() 
{ 
  std::vector<Int> v; 

   Int x3 = foldl(v.begin(), v.end(), intfunc, Int(0)); 
   Int x4 = foldl(v.cbegin(), v.cend(), intfunc, Int(0)); 



} 


When I compile this on VC++2010, I get warning: 
Warning 1       warning C4172: returning address of local variable or 
temporary       c:\users\jensa\desktop\vs2010bug\foldl\foldl\foldl.cpp  18 
Warning 2       warning C4172: returning address of local variable or 
temporary       c:\users\jensa\desktop\vs2010bug\foldl\foldl\foldl.cpp  18 
Sign in to post a comment.
Posted by Microsoft on 2/20/2012 at 5:02 PM
Hello,

Thank you for reporting this bug. We confirmed that it is a bug, and we've fixed it for the next major release of Visual Studio.

Thanks,
Ulzii Luvsanbat
Visual C++ Team
Posted by MS-Moderator08 [Feedback Moderator] on 2/16/2012 at 2:28 AM
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 jensa79 on 2/16/2012 at 12:26 AM
I posted the example in comp.lang.c++.moderated and Daniel Krüger commented:
IMO there is nothing wrong in your code, decltype(f(initial, *first))
should be deduced to be equal to Int, which is exactly the same type
that you are returning. Testing with VS2010 it seems that

static_assert(std::is_same<decltype(f(initial, *first)), T>::value,"");


this test fails pointing to the root of the compiler error. It turns
out, that the wrong result


static_assert(std::is_same<decltype(f(initial, *first)), T&>::value,"");


is accepted. This is definitively a compiler error, the function intfunc
returns an Int and this type is required to be returned by decltype.