Search code examples
c++c++11lambdafunctornegation

Is There a Way to Just Evaluate the Argument to a Functor?


Given foo which is a vector I want to evaluate it's contents with all_of. But all I'm really trying to check is that each element evaluates to true.

I can do this by using logical_not and none_of but I would rather not use double negatives, and it feels dumb to write a lambda: [](const auto param) -> bool { return param; }

Does the standard provide me a functor that does what I want?


Solution

  • What you are looking for is std::identity which was added to C++20. It takes a parameter and returns it unchanged. It operator() looks like

    template<typename T>
    constexpr T&& operator()( T&& t ) const noexcept;
    

    and it returns

    std::forward<T>(t)