Created attachment 322 [details] 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; } ---------------------------------------------------------
Created attachment 323 [details] New Proposed Patch Sorry, the previous patch was severely broken. (I just didn't notice that, in make check, my added -std=c++11 is invalidated by -ansi... shame... ) Please find the new patch, which makes a minimal change to the Core/util/Meta.h Patch includes some change for unsupported sections, which are required to make it compilable with c++11 mode.
I am having this same problem (reported almost three years ago). I can't tell if it's been fixed in one of the versions, but it's not fixed in mine (3.2.0.8), but I did find a good workaround if other googlers end up here: Wrapping the lambda with std::function<> worked. i.e.: auto B = A.unaryExpr( std::function<int(string)>([](string s)-> int { return atoi( s.c_str() ); } ));
See also bug 1006, and bug 96 for discussions on result_of.
Actually, the initial example works fine in 3.3beta1. The fix is in the following changeset: https://bitbucket.org/eigen/eigen/commits/a1bdc556d There is no plan to improve C++11 support in the 3.2 branch.
-- GitLab Migration Automatic Message -- This bug has been migrated to gitlab.com's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.com/libeigen/eigen/issues/577.