This bugzilla service is closed. All entries have been migrated to https://gitlab.com/libeigen/eigen
Bug 577 - C++11 lambda support
Summary: C++11 lambda support
Status: RESOLVED FIXED
Alias: None
Product: Eigen
Classification: Unclassified
Component: Core - general (show other bugs)
Version: unspecified
Hardware: All All
: Normal Unknown
Assignee: Nobody
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 3.3
  Show dependency treegraph
 
Reported: 2013-03-27 11:16 UTC by Takaki Makino
Modified: 2019-12-04 12:13 UTC (History)
3 users (show)



Attachments
Proposed Patch for Eigen 3.2-beta1 (18.14 KB, patch)
2013-03-27 11:16 UTC, Takaki Makino
no flags Details | Diff
New Proposed Patch (18.65 KB, patch)
2013-03-27 13:26 UTC, Takaki Makino
no flags Details | Diff

Description Takaki Makino 2013-03-27 11:16:56 UTC
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;
}
---------------------------------------------------------
Comment 1 Takaki Makino 2013-03-27 13:26:35 UTC
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.
Comment 2 Jeff Eberl 2016-01-15 17:51:10 UTC
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() ); } ));
Comment 3 Gael Guennebaud 2016-01-21 13:59:09 UTC
See also bug 1006, and bug 96 for discussions on result_of.
Comment 4 Gael Guennebaud 2016-01-21 14:42:20 UTC
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.
Comment 5 Nobody 2019-12-04 12:13:06 UTC
-- 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.

Note You need to log in before you can comment on or make changes to this bug.