函数包装器via(function object)类(variadic)模板 [英] Function wrapper via (function object) class (variadic) template

查看:195
本文介绍了函数包装器via(function object)类(variadic)模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++



我试图通过(函数对象)类(variableadic)模板实现函数包装。类具有作为其唯一的数据成员的函数指针,该函数指针由被包装的函数指针初始化或分配。参数化构造函数接受一个函数指针并通过它初始化成员。 operator()方法接受参数(或无),并使用它们调用包装函数。至少这是这个想法。我得到许多错误,我标记与评论。 VC11(与2012年11月CTP,启用可变参数模板)给我错误C2091:函数返回function 除了一个标记的区域。最后一个错误是不同的,我在代码中注释它的完整描述。 g ++给出大部分相同的错误,尽管代码数不同。

  #include< iostream> 

template< typename R,typename ... Tn>
class func
{
R(* fptr)(Tn ...); // C2091
public:
func():fptr(nullptr){}
func(R(* f)(Tn ...)):fptr(f){} // C2091
R operator()(Tn ... args)
{// C2091
return fptr(args ...);
}
func& operator =(R(* f)(Tn ...))// C2091
{
fptr = f;
return * this;
}
};

int foo(int a,int b)
{
std :: cout< foo\\\
;
return 0;
}

int main()
{
func< int(int,int)> myfunc;
myfunc = foo; // C2679:binary'=':没有操作符,它接受
//类型为'int(__cdecl *)(int,int)'(或
)的右手操作数//没有可接受的转换)
}

为什么会收到这些错误?例如,我不知道参数化构造函数如何返回任何东西,或者数据成员的声明如何返回任何东西。是不是数据成员声明的形式是一个函数指针声明?例如, int(* g)(int); 声明一个指向指向一个 int 并返回 int



编辑/附录: >

我从答案中看到 int(int,int)只是一个类型,我需要部分专门化效果我想。但是,什么产生错误在我的代码?如果我注释掉 myfunc = foo ,我仍然得到其他错误。 func< int(int,int)> myfunc; 调用默认构造函数。 typename R 实例化为 int(int,int) typename ... Tn 变为空。数据成员 R(* fptr)(Tn ...); 变为 R(* fptr)(); ,因此 fptr 是一个函数指针,指向一个取零参数并返回 R 的函数。如果 R int(int,int),则 R 函数指针类型或函数类型?如果是后者,那么我可以理解错误消息的上下文。

解决方案

int ,int)是一个单一类型。如果你想传递它并解开它,你需要部分专门化:

  template< typename> struct func; // leave undefined 

template< typename R,typename ... Args>
struct func< R(Args ...)> //专用于typename = R(Args ...)
{
// ...
};


C++

I'm trying to implement a function wrapper via a (function object) class (variadic) template. The class has as its only data member a function pointer that is initialized by or assigned the function pointer it is wrapping. The parametrized constructor takes a function pointer and initializes the member by it. The operator() method takes argument(s) (or none) and calls the wrapped function with them. At least that's the idea. I get many errors, which I mark with comments. VC11 (with the November 2012 CTP, to enable variadic templates) gives me error C2091: function returns function in all but one of the marked areas. The last error is different, and I comment its full description in the code. g++ gives mostly the same errors, albeit with different code numbers.

#include <iostream>

template <typename R, typename... Tn>
class func
{
    R (*fptr)(Tn...); // C2091
public:
    func() : fptr(nullptr) {}
    func( R (*f) (Tn...) ) : fptr(f) {} // C2091
    R operator()(Tn... args)
    { // C2091
        return fptr(args...);
    }
    func& operator=( R (*f) (Tn...) ) // C2091
    {
        fptr = f;
        return *this;
    }
};

int foo(int a, int b)
{
    std::cout << "foo\n";
    return 0;
}

int main()
{
    func<int(int, int)> myfunc;
    myfunc = foo; // C2679: binary '=' : no operator found which takes
    // a right-hand operand of type 'int (__cdecl *)(int,int)' (or 
    // there is no acceptable conversion)
}

Why am I getting these errors? For example, I don't see how the parametrized constructor returns anything, or how the declaration of the data member returns anything. Isn't the data member declaration in the form of a function pointer declaration? For example, doesn't int (*g)(int); declare a pointer that points to a function that takes an int and returns an int?

Edit/Addendum:

I see from the answers that int(int, int) is only one type and that I need partial specialization to get the effect I want. But, what produces the error in my code? If I comment out myfunc = foo, I still get the other errors. func<int(int, int)> myfunc; calls the default constructor. typename R gets instantiated to int(int, int), and typename... Tn becomes empty. The data member R (*fptr)(Tn...); becomes R (*fptr)();, and fptr is therefore a function pointer that points to a function that takes zero arguments and returns an R. If R is int(int, int), then is R a function pointer type or a function type? If it's the latter, then I can understand the context of the error message.

解决方案

int(int, int) is one single type. If you want to pass it like that and unwrap it, you need partial specialization:

template <typename> struct func;         // leave undefined

template <typename R, typename ...Args>
struct func<R(Args...)>                  // specialized for typename = R(Args...)
{
    // ...
};

这篇关于函数包装器via(function object)类(variadic)模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆