相关疑难解决方法(0)

只返回传递值的默认函数?

作为一个懒惰的开发人员,我喜欢使用这个技巧来指定一个默认函数:

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);
}
Run Code Online (Sandbox Code Playgroud)

但是在一个非常特殊的情况下我遇到了一个问题,如下所示:

template <class Type, unsigned int Size, class Function = /*SOMETHING 1*/>
void index(std::array<Type, Size> &x, Function&& f = /*SOMETHING 2*/)
{
    for (unsigned int i = 0; i < Size; ++i) {
        x[i] = f(i);
    }
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我希望默认函数相当于:( [](const unsigned int i){return i;}一个只返回传递值的函数).

为了做到这一点,我需要写什么而不是/*SOMETHING 1*//*SOMETHING 2*/

c++ lambda templates c++11 std-function

13
推荐指数
1
解决办法
5250
查看次数

标签 统计

c++ ×1

c++11 ×1

lambda ×1

std-function ×1

templates ×1