C++11 lambda support
Submitted by Takaki Makino
Assigned to Nobody
Link to original bugzilla bug (#577)
Description
Created attachment 322
Proposed Patch for Eigen 3.2-beta1
C++11 supports lambda expression, which is quite useful if we combine it with Eigen. For example, Matrix::unaryExpr can handle many types of nontrivial operations without defining functional classes. Please see the example code at the end.
Current Eigen implementation does not support lambda expression
to be specified in unaryExpr if the argument type and the return type are different. This is because Eigen::internal::result_of cannot handle
the lambda expression correctly, and falling back to the last-resort routine,
in which the return type is assumed as the first argument type.
A comment in the source code says "FIXME, that behavior is a pretty bad hack" so it is worth improving the code.
With assuming C++11, we can replace the implementation of result_of as well as most of other meta-classes in Meta.h with those defined in standard <type_traits> header, which is cleaner and (hopefully) compiled more effiently.
Unfortunately, it turned out that many other codes in Eigen relies on the "bad hack" behavior of the result_of, so we also need to fix these too.
Attached please find a proposed patch (for Eigen 3.2-beta1) that makes the required modification.
With the patch the example can be happily compiled with g++ -std=c++0x, and "make check" results are not so changed (tested with g++ 4.8.0).
I'd appreciate very much if you could include the patch in the next release.
#include <Eigen/Dense>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace Eigen;
using namespace std;
int main()
{
Array< string, Dynamic, Dynamic > A(2,3);
A << "123", "234", "456", "345", "789", "567";
auto B = A.unaryExpr( [](string s)-> int { return atoi( s.c_str() ); } );
cout << B << endl;
}
Patch 322, "Proposed Patch for Eigen 3.2-beta1":
eigen.patch.1