C ++用于转换为函数指针的转换运算符 [英] C++ Conversion operator for converting to function pointer

查看:221
本文介绍了C ++用于转换为函数指针的转换运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在磨练我的头,反对一个在我的头上很简单的想法,但我不知道如何在C ++中实现。 ,我可以在这个简单的例子中声明一个带有转换运算符的类:

  class Foo 
{
private:

int _i;

public:

Foo(int i):_i(i){}

操作符int()const
{
return i;
}
};

现在我可以写出真棒的东西,比如

  int i = Foo(3); 

但是在我的特殊情况下,我想提供一个操作符将对象转换为函数指针(例如将 Bar 实例转换为 int(*)(int,int)函数指针)。这是我最初尝试的:

  class Bar 
{
private:

int(* _funcPtr)(int,int);

public:

Bar(int(* funcPtr)(int,int)):_funcPtr(funcPtr){}

operator int )(int,int)()const
{
return _funcPtr;
}

};

但是运算符函数无法编译,会生成以下错误:

 '*'标记之前的期望标识符
'< invalid-operator>'声明为返回函数的函数

我也尝试过上面的简单变体,例如在括号中包含返回类型,但所有这些想法也失败了。 / p>

有没有人知道用于声明转换到函数指针运算符方法的语法,或者是否甚至可以这样做?



注意:我使用GCC 4.5.2编译这个代码:: Blocks。也欢迎包含一些新的C ++ 0x概念的答案。



编辑



简化示例,我无意中遗漏了一个细节。这有点奇怪,但是不是严格返回一个 int(*)(int,int)指针,转换操作符是模板:

  template< typename ReturnType,typename ArgType1,typename ArgType2> 
operator ReturnType(*)(ArgType1,ArgType2)()const
{
//实现在这里不重要
}
pre>

据我所知,我不能再typedef这样的类型。

解决方案

因为你必须知道:

p>

 (* operator int()const)(int,int)
{
return _funcPtr;
}

(再次修正。)






更新:我收到Johannes Schraub和Luc Danton的通知,这个语法实际上是无效的, strong>必须使用typedef。因为你说typedef不是一个选项,这里是一个辅助类,可以包装你的typedef:

  template< typename R,类型名称A1,类型名称A2> 
struct MakeFunctionType
{
typedef R(* type)(A1,A2);
};

template< typename R,typename A1,typename A2>
operator typename MakeFunctionType< R,A1,A2> :: type()const
{
//实现在这里不重要
}


I'm been grinding my head against an idea that is simple enough in my head, but I can't figure out how to implement in C++.

Normally, I can declare a class with a conversion operator like in this simple example:

class Foo
{
private:

    int _i;

public:

    Foo( int i ) : _i(i) { }

    operator int( ) const
    {
        return i;
    }
};

So now I can write awesome stuff like

int i = Foo(3);

But in my particular case, I would like to provide an operator for converting an object to a function pointer (e.g. converting a Bar instance to a int(*)(int, int) function pointer). Here's what I initially tried:

class Bar
{
private:

    int (*_funcPtr)(int, int);

public:

    Bar( int (*funcPtr)(int, int) ) : _funcPtr(funcPtr) { }

    operator int(*)(int, int) ( ) const
    {
        return _funcPtr;
    }

};

But the operator function fails to compile, with these errors being generated:

expected identifier before '*' token
'<invalid-operator>' declared as a function returning a function

I have also tried simple variations on the above, such as surrounding the return type in parenthesis, but all these ideas have also failed.

Does anyone know what the syntax is for declaring a conversion-to-function-pointer operator method, or whether it is even possible to do so?

Note: I am compiling this with Code::Blocks using GCC 4.5.2. Answers involving some of the new C++0x concepts are also welcome.

Edit

In my strive for simplifying the example, I unintentionally left out one detail. It's a bit weird, but rather than strictly returning an int(*)(int,int) pointer, the conversion operator is intended to be templated:

template<typename ReturnType, typename ArgType1, typename ArgType2>
operator ReturnType(*)(ArgType1, ArgType2) ( ) const
{
    // implementation is unimportant here
}

As far as I know, I no longer cannot typedef such a type. This clearly makes things much more clumsy, but I hope that there is still a way.

解决方案

Since you must know:

(*operator int() const)(int, int)
{
  return _funcPtr;
}

(Fixed. Again.)


Update: I've been informed by Johannes Schraub and Luc Danton that this syntax is in fact not valid, and that you really must use a typedef. Since you say that typedefs aren't an option, here's a helper class that can wrap your typedef:

template<typename R, typename A1, typename A2>
struct MakeFunctionType
{
  typedef R(*type)(A1, A2);
};

template<typename R, typename A1, typename A2>
operator typename MakeFunctionType<R, A1, A2>::type () const
{
    // implementation is unimportant here
}

这篇关于C ++用于转换为函数指针的转换运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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