Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7944 Discussions

std::tr1::result_of support with the lambdas?

jlperla
Beginner
1,525 Views

As far as I can tell, C++0X lambdas on Intel 11.1 Windows do not support the result_type protocols used by result_of (i.e. a nested result_type or a nested result template). See the following code:

auto l1 = [](double x){return 1.0;};
typedef decltype(l1) l1_type;
static_assert(std::tr1::is_same<:TR1::RESULT_OF>::type, double>::value, "Failure of result_of"); //Doesn't work...
-Jesse

0 Kudos
7 Replies
Quoc-An_L_Intel
Moderator
1,525 Views

Thanks. I'll update this thread when we have a resolution.

0 Kudos
Judith_W_Intel
Employee
1,525 Views

It looks like you areusing the std::tr1::result_of from the standard C++ library (their example isnt complete sinceyou dont have an #include of but I think I recreated an example). It looks like thecode doesnt meet the requirements for use with result_of, i.e. look at http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1454.html where it says the following below. The lambda/function object has neither a result_type nor a nested result member class template. This is exactly what the diagnostic I am seeing says, i.e.

F:/Program Files/Microsoft Visual Studio 10.0/VC/include/xxresult(28): error: class "lambda [](double)->double" has no member "result"

The result_of class template follows the existing practice. It defines a namespace-level class template (result_of) that determines the return type of a function object given the argument types to be passed to that function object, and defines two methods for retrieving the function call operator return type from the function object type: the first uses result_type, for backward compatibility with simple function objects, and the second uses a nested result member class template, for use with more powerful function objects.

I see similar errors from the MSVC++ 2010 compiler.

Judy

0 Kudos
jlperla
Beginner
1,525 Views

Thanks.

I think that what I was thinking is that the lambda functors should provide a nested result_type to interface properly with the result_of template? result_of works for both functors and functions and I think is intended to be a uniform way to get at the return type. Since lambdas are currently monomorphic, it seems plausible (and maybe consistent with the C++0X standard? But there I could be completely wrong) for them to play better with this part of the standard library.

0 Kudos
Judith_W_Intel
Employee
1,525 Views
I don't see anything in the standard that says result_ofmust work with lambdas. The link above implies that the standardlibrary in the future could use typeof and variadic templatesto have result_of work automatically with function objects (i.e. no requirements would be made on the function object) but the Microsoft compiler hasn't implemented these features yet.
0 Kudos
jlperla
Beginner
1,525 Views

Thanks for looking at this.

Perhaps you are right and it is not required... but since so much code is written (in the STL and otherwise) to use the nested typedef of result_type for functors, isn't it a practical requirement for lambdas? And since they are monomoprhic, couldn't the compiler just generate the nested typedef for this with the result_type it already knows? Just a thought as I have had to run through hoops to get the lambdas to work in code that works with every other function and functor type.

Thanks,

Jesse

0 Kudos
Judith_W_Intel
Employee
1,525 Views
Yes, I think the compiler could implement this if it was required. I would suggest submitting a public comment to the C++ standards committee (it's probablytoo late for C++0x) with this suggestion. Also if you would like to enter a feature request for the Intel C++ compiler to support this usageplease do so.
0 Kudos
jlperla
Beginner
1,525 Views
I submitted a note to comp.lang.c++.std. Please put in a feature request tot he compiler guys.

Thanks as always Judith.

-Jesse

0 Kudos
Reply