Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 只返回传递值的默认函数?_C++_Templates_C++11_Lambda_Std Function - Fatal编程技术网

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

C++ 只返回传递值的默认函数?,c++,templates,c++11,lambda,std-function,C++,Templates,C++11,Lambda,Std 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);

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

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);
}
模板
空排列(std::array&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);
    }
}
模板
空索引(std::数组和x,函数和f=/*2*/)
{
for(无符号整数i=0;i
在本例中,我希望默认函数等效于:
[](const unsigned int I){return I;}
(仅返回传递值的函数)

为了做到这一点,我必须写什么来代替
/*某物1*/
/*某物2*/

这就是函数。不幸的是,它不是C++标准的一部分,但你可以很容易地自己构建一个。
如果您碰巧使用了g++,您可以使用
-std=gnu++11
激活它的扩展,然后

#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);
    }
}

#包括。在此之前,您可以查看boost的版本。

您只需构建自己的身份函子:

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);
  }
}
模板
类ReturnIdentificationFunctor
{
公众:
自动运算符()(T&&i)->decltype(std::forward(i))
{
返回std::move(i);
}
};
模板
空索引(std::数组和x,函数和f=Function())
{
for(无符号整数i=0;i
没有标准的函子可以做到这一点,但它很容易编写(尽管确切的形式有争议):

结构标识{
模板
constexpr自动运算符()(U&v)const noexcept
->decltype(std::forward(v))
{
返回标准::向前(v);
}
};
这可以按如下方式使用:

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);
    }
}
模板
空索引(std::数组和x,函数和f=Function())
{
for(无符号整数i=0;i
boost::phoenix提供了一个完整的功能工具箱,这里的“arg1”是标识到标识的标识;-)

#包括
模板
无效索引(X&X,函数f=Function()){
对于(std::size_t i=0;i
解决这一问题的方法是使用两种不同的功能。我发现不使用默认参数是明智的

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
}
模板
空索引(std::数组和x、函数和f){
对于(无符号整数i=0;i
有以下带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))
模板
空索引(std::array&x,Function&&f=boost::bind(std::plus(),0,_1))

@DeadMG谢谢你,评论不错,我用我认为最基本的答案编辑了我的答案,你能告诉我这是否解决了所有问题吗。我应该使用
forward
而不是
move
?+1,而是使用
Function()
而不是
identity()
作为默认参数。为什么在这里使用结构?为什么不将
identity
定义为函数本身呢?@Claudiu:struct可以作为对象传递到元函数中(这意味着可以对模板参数进行类型推断,也意味着编译器更容易内联)。裸函数必须作为函数指针传递。要将函数模板转换为函数指针,必须手动实例化模板(使用可能未知的类型参数)。“足够简单”。在这里,默认参数也应该是
Function()
(即使
Function
不是
\ugnu\ucxx::identity
),也允许它工作。C++14版本很有趣。为什么我们要切换到一段更长、更难阅读的代码:D@LightnessRacesinOrbit,这只是为了说明一个更一般的上下文,其中需要一个真正的身份、通用的转发功能
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
}
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))