Default function that just returns the passed value?

前端 未结 6 1517
礼貌的吻别
礼貌的吻别 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:08

    You can just build your own identity functor:

    template 
    class returnIdentifyFunctor
    {
      public:
         auto operator ()(  T &&i ) -> decltype( std::forward(i) )
        {
          return std::move(i);
        }
    };
    
    template >
    void index(std::array &x, Function&& f = Function() )
     {
        for (unsigned int i = 0; i < Size; ++i) {
                x[i] = f(i);
      }
    }
    

提交回复
热议问题