Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filledFrom using lambda function #2037

Merged
merged 2 commits into from
May 26, 2020
Merged

filledFrom using lambda function #2037

merged 2 commits into from
May 26, 2020

Conversation

bendudson
Copy link
Contributor

Simplifies a common pattern

Field3D result{emptyFrom(field)};
BOUT_FOR(i, result) {
  result[i] = ...;
}

and allows it to be used as an expression:

auto result = filledFrom(field, [&](const auto& i) {
                                  return ...;
                                });

Examples of use: https://github.com/bendudson/hermes-3/blob/master/src/collisions.cxx#L106

Simplifies a common pattern
```
Field3D result{emptyFrom(field)};
BOUT_FOR(i, result) {
  result[i] = ...;
}
```
and allows it to be used as an expression
```
auto result = filledFrom(field, [&](const auto& i) {
                                  return ...;
                                });
```
Compiling with OpenMP fails due to asserts with an exit from loops
Copy link
Member

@ZedThree ZedThree left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @bendudson ! A very nifty solution!

/// An optional third argument is the region string
template <
typename T, typename Function,
typename = decltype(std::declval<Function&>()(std::declval<typename T::ind_type&>()))>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, C++! 😄 The only bit I'm not sure of is the reference in declval<Function&> -- what am I missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also a bit unclear on this magic incantation. It's adapted from https://stackoverflow.com/questions/50859115/enable-if-template-param-is-lambda-with-particular-signature

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, it's to guard against strange types! If F had a call operator like void operator(ind_type) &&, which is rvalue qualified, it can only be called on a temporary. But we definitely have an lvalue here, that is, a named variable. F& is a mutable reference, so it must be an lvalue, so it ensures that this will definitely be the right operator() overload.

Phew!

This might be a FIXME for when we get to move to C++17 and it can be std::is_invocable

@ZedThree ZedThree merged commit d2457b4 into next May 26, 2020
@ZedThree ZedThree deleted the filledfrom-lambda branch May 26, 2020 09:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants