【发布时间】:2013-02-18 14:17:05
【问题描述】:
作为一个懒惰的开发者,我喜欢用这个技巧来指定一个默认函数:
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);
}
但我在一个非常特殊的情况下遇到了问题,如下所示:
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);
}
}
在这种情况下,我希望默认函数相当于:[](const unsigned int i){return i;}(一个只返回传递值的函数)。
为了做到这一点,我必须写什么来代替/*SOMETHING 1*/ 和/*SOMETHING 2*/?
【问题讨论】:
标签: c++ templates c++11 lambda std-function