Default function that just returns the passed value?

前端 未结 6 1474
礼貌的吻别
礼貌的吻别 2020-12-31 08:24

As a lazy developer, I like to use this trick to specify a default function:

template 

        
相关标签:
6条回答
  • 2020-12-31 09:02

    A way to deal with this is to have two different functions. I find quite sane not to use default parameters.

    template <class Type, unsigned int Size, class Function>
    void index(std::array<Type, Size> &x, Function&& f){
        for(unsigned int i = 0; i < Size; ++i) x[i] = f(i);
    }
    
    template<class Type, unsigned int Size>
    void index(std::array<Type, Size> &x){
        return index(x, [](unsigned int i){return i;});                      // C++11 in this case
                    //, [](auto&& e){return std::forward<decltype(e)>(e)};); // C++14 in a more general case
                    //, std::identity); // C++20 in general
    }
    
    0 讨论(0)
  • 2020-12-31 09:08

    You can just build your own identity functor:

    template <typename T>
    class returnIdentifyFunctor
    {
      public:
         auto operator ()(  T &&i ) -> decltype( std::forward<T>(i) )
        {
          return std::move(i);
        }
    };
    
    template <class Type, unsigned int Size, class Function = returnIdentifyFunctor<Type>>
    void index(std::array<Type, Size> &x, Function&& f = Function() )
     {
        for (unsigned int i = 0; i < Size; ++i) {
                x[i] = f(i);
      }
    }
    
    0 讨论(0)
  • 2020-12-31 09:13

    There is no standard functor that does this, but it is easy enough to write (though the exact form is up for some dispute):

    struct identity {
        template<typename U>
        constexpr auto operator()(U&& v) const noexcept
            -> decltype(std::forward<U>(v))
        {
            return std::forward<U>(v);
        }
    };
    

    This can be used as follows:

    template <class Type, std::size_t Size, class Function = identity>
    void index(std::array<Type, Size> &x, Function&& f = Function())
    {
        for (unsigned int i = 0; i < Size; ++i) {
            x[i] = f(i);
        }
    }
    
    0 讨论(0)
  • 2020-12-31 09:14

    This is called the identity function. Unfortunately, it is not part of the C++ standard, but you can easily build one yourself.


    If you happen to use g++, you can activate its extensions with -std=gnu++11 and then

    #include <array>
    #include <ext/functional>
    
    template <class Type, std::size_t Size, class Function = __gnu_cxx::identity<Type> >
    void index(std::array<Type, Size> &x, Function&& f = Function())
    {
        for (unsigned int i = 0; i < Size; ++i) {
            x[i] = f(i);
        }
    }
    

    Maybe it will be available in C++20, see std::identity. Until then you may look at boost's version at boost::compute::identity.

    0 讨论(0)
  • 2020-12-31 09:14

    boost::phoenix offers a complete functional toolbox, here 'arg1' is the ident to identity ;-)

    #include <boost/phoenix/core.hpp>
    
    template <class X, class Function = decltype(boost::phoenix::arg_names::arg1)>
    void index(X &x, Function f = Function()) {
        for (std::size_t i = 0; i < x.size(); ++i) {
                x[i] = f(i);
      }
    }
    
    0 讨论(0)
  • 2020-12-31 09:17

    There is the following variant with boost:

    template <class Type, unsigned int Size, class Function = boost::function<Type(Type)>>
    void index(std::array<Type, Size> &x, Function&& f = boost::bind(std::plus<Type>(), 0, _1))
    
    0 讨论(0)
提交回复
热议问题