Skip to content
Snippets Groups Projects
Commit f90c50ee authored by Carsten Gräser's avatar Carsten Gräser
Browse files

[bugfix] Fix Dune::Hybrid::Impl::Id

There's a bug in the current implementation of `Dune::Hybrid::Impl::Id`.
Trying to do 'perfect forwarding' using

  template<class T>
  constexpr decltype(auto) foo()(T&& x) {
    return std::forward<T>(x);
  }

leads to undefined behaviour if you pass an r-value
to the function. Then `decltype(auto)` would be
deduced to be an r-value reference such that in

  auto&& bar = foo(expression());

`bar` would be bound to a temporary which is no longer
alive because there's no lifetime prolongation. The correct
version is

  template<class T>
  constexpr T foo()(T&& x) {
    return std::forward<T>(x);
  }

When passed an r-value of the raw type `E` then `T` is
deduced to be `E` such that we move the temporary into
the new value `bar`. If we pass an l-value of the raw
type `E`, then `T` is deduced to be either `T&` or
`const T&` such that we properly return the reference.
parent 07b27021
Branches
Tags
1 merge request!665[bugfix] Fix Dune::Hybrid::Impl::Id
Pipeline #18290 passed
......@@ -301,7 +301,7 @@ namespace Impl {
struct Id {
template<class T>
constexpr decltype(auto) operator()(T &&x) const {
constexpr T operator()(T&& x) const {
return std::forward<T>(x);
}
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment