Calling const mutable lambdas

To simplify the test test, suppose I have the following shell class:

template <typename T>
struct Wrapper {
  decltype(auto) operator()() const {
    return m_t();
  }
  decltype(auto) operator()() {
    return m_t();
  }
  T m_t;
};

template <typename T>
auto make_wrapper(T t) {
  return Wrapper<T>{t};
}

And let's say I wrap the following trivial functor returning links:

struct Foo {
  int& operator()() {
    return x;
  }
  const int& operator()() const {
    return x;
  }
  int x;
};

In my function, mainI am trying to turn a functor Foointo a lambda closure. Since I want it to return non-constant links, I install it mutableand using decltype(auto):

int main() {
  Foo foo;
  auto fun = [foo]() mutable -> decltype(auto) { return foo(); };
  auto wfun = make_wrapper(fun);
  const auto& cwfun = wfun;

  wfun();     // <- OK
  cwfun();    // <- BAD!
}

For the second call cwfun(), the first constversion is called Wrapper::operator(), but there it is m_tconsidered as constlambda and, therefore, cannot be called. I suppose this is because it m_twas noted mutablefirst. So what would be a good way to make this work? Convert m_tto non-t28 before calling it operator() const?

Goals

, cwfun() Wrapper::operator() const Foo::operator() const. Wrapper::m_t mutable, , Foo::operator() Foo::operator() const.

const Wrapper::operator() const, , Foo::operator() Foo::operator() const . - :

return const_cast<typename std::add_lvalue_reference<typename std::add_const<typename std::remove_reference<decltype(m_t())>::type>::type>::type>(m_t());

, .

Coliru

, clang, :

tc-refptr.cc:8:12: error: no matching function for call to object of type 'const (lambda at
      tc-refptr.cc:40:14)'
    return m_t();
           ^~~
tc-refptr.cc:44:27: note: in instantiation of member function 'Wrapper<(lambda at
      tc-refptr.cc:40:14)>::operator()' requested here
  DebugType<decltype(cwfun())> df;
                          ^
tc-refptr.cc:40:14: note: candidate function not viable: 'this' argument has type 'const
      (lambda at tc-refptr.cc:40:14)', but method is not marked const
  auto fun = [foo]() mutable -> decltype(auto) { return foo(); };

Coliru

+4
1

partial_apply, const-:

template<class F, class...Args>
struct partial_apply_t {
  std::tuple<Args...> args;
  F f;
  template<size_t...Is, class Self, class...Extra>
  static auto apply( Self&& self, std::index_sequence<Is...>, Extra&&...extra )
  -> decltype(
    (std::forward<Self>(self).f)(
      std::get<Is>(std::forward<Self>(self).args)...,
      std::declval<Extra>()...
    )
  {
    return std::forward<Self>(self).f(
      std::get<Is>(std::forward<Self>(self).args)...,
      std::forward<Extra>(extra)...
    );
  }
  partial_apply_t(partial_apply_t const&)=default;
  partial_apply_t(partial_apply_t&&)=default;
  partial_apply_t& operator=(partial_apply_t const&)=default;
  partial_apply_t& operator=(partial_apply_t&&)=default;
  ~partial_apply_t()=default;
  template<class F0, class...Us,
    class=std::enable_if_t<
      std::is_convertible<std::tuple<F0, Us...>, std::tuple<F, Args...>>{}
    >
  >
  partial_apply_t(F0&& f0, Us&&...us):
    f(std::forward<F0>(f0)),
    args(std::forward<Us>(us)...)
  {}
  // three operator() overloads.  Could do more, but lazy:
  template<class...Extra, class Indexes=std::index_sequence_for<Extra>>
  auto operator()(Extra&&...extra)const&
  -> decltype( apply( std::declval<partial_apply_t const&>(), Indexes{}, std::declval<Extra>()... ) )
  {
    return apply( *this, Indexes{}, std::forward<Extra>(extra)... );
  }
  template<class...Extra, class Indexes=std::index_sequence_for<Extra>>
  auto operator()(Extra&&...extra)&
  -> decltype( apply( std::declval<partial_apply_t&>(), Indexes{}, std::declval<Extra>()... ) )
  {
    return apply( *this, Indexes{}, std::forward<Extra>(extra)... );
  }
  template<class...Extra, class Indexes=std::index_sequence_for<Extra>>
  auto operator()(Extra&&...extra)&&
  -> decltype( apply( std::declval<partial_apply_t&&>(), Indexes{}, std::declval<Extra>()... ) )
  {
    return apply( std::move(*this), Indexes{}, std::forward<Extra>(extra)... );
  }
};
template<class F, class... Ts>
partial_apply_t<std::decay_t<F>, std::decay_t<Ts>...>
partial_apply(F&& f, Ts&&...ts) {
  return {std::forward<F>(f), std::forward<Ts>(ts)...};
}

:

auto fun = partial_apply(
  [](auto&& foo) -> decltype(auto) { return foo(); },
  foo
);

foo partial_apply, , , ( const-) . , foo fun.

, , , , , , std::ref , , args, std::reference_wrapper .

: a reference_unwrapper, std::reference_wrapper s.

partial_apply decay_t ing.

+1

All Articles