c++ - Default function that just returns the passed value? -


as lazy developer, use trick specify default function:

template <class type, unsigned int size, class function = std::less<type> > void arrange(std::array<type, size> &x, function&& f = function()) {     std::sort(std::begin(x), std::end(x), f); } 

but have problem in particular case, following:

template <class type, unsigned int size, class function = /*something 1*/> void index(std::array<type, size> &x, function&& f = /*something 2*/) {     (unsigned int = 0; < size; ++i) {         x[i] = f(i);     } } 

in case, default function equivalent of: [](const unsigned int i){return i;} (a function returns passed value).

in order that, have write instead of /*something 1*/ , /*something 2*/?

there no standard functor this, easy enough write (though exact form 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 used follows:

template <class type, std::size_t size, class function = identity> void index(std::array<type, size> &x, function&& f = function()) {     (unsigned int = 0; < size; ++i) {         x[i] = f(i);     } } 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -