如何在模板实例化时确定函数的返回类型?

发布于 2024-11-16 06:35:34 字数 1105 浏览 5 评论 0原文

有没有办法找出函数的返回类型(或者更好的函数指针)是什么?

我有以下代码,当我使用 void 函数指针时,该代码会中断,但对于任何其他返回类型都可以正常工作。使用 gcc 4.5 中的 void 函数指针时出现的错误是:

错误:无效值没有被忽略,因为它应该是

这是有道理的。所以显然我需要检查函数指针是否返回某些内容,然后获取结果并返回它。

template <class F,typename R> class Instruction
{
  protected:
    F (func);
    R result;

  public:

   template <typename T>
   Instruction(T const& f)
   {
     func = &f;
   };

   template <typename A> void execute(A const& a)
   {
     result = (func)(a);
   };
   template <typename A> void execute(A const& a,A const& b)
   {
     result = (func)(a,b);
   };
   template <typename A> void execute(A const& a,A const& b,A const& c)
   {
     result = (func)(a,b,c);
   };
   R get_result()
   {
     return result;
   }; 
};

通常,我使用指向函数的函数指针,该函数主要执行算术运算,并且我可以在实例化时处理除 void 函数之外的任何返回类型。我尝试实例化为:

     Instruction<ptr2func2,void> foo(bar);
     Instruction<ptr2func2,(*void)> foo(bar);

但在两种情况下都失败了。

实例化时的第二个模板参数用于定义返回类型。

Is there a way to find out what the return type of a function (or even better of a function pointer) is ?

I've got the following code, which breaks when I use a void function pointer, but works fine for any other return type. The error I get when using a void function pointer from gcc 4.5 is:

error: void value not ignored as it ought to be

Which makes sense. So obviously I need to check if function pointer returns something, and then obtain the result and return it.

template <class F,typename R> class Instruction
{
  protected:
    F (func);
    R result;

  public:

   template <typename T>
   Instruction(T const& f)
   {
     func = &f;
   };

   template <typename A> void execute(A const& a)
   {
     result = (func)(a);
   };
   template <typename A> void execute(A const& a,A const& b)
   {
     result = (func)(a,b);
   };
   template <typename A> void execute(A const& a,A const& b,A const& c)
   {
     result = (func)(a,b,c);
   };
   R get_result()
   {
     return result;
   }; 
};

Normally I use a function pointer to a function which does something mostly arithmetic and I can take care of any return type at instantiation other than void functions. I've tried instantiating as:

     Instruction<ptr2func2,void> foo(bar);
     Instruction<ptr2func2,(*void)> foo(bar);

but in both cases it fails.

The second template argument at instantiation is used in order to define what the return type will be.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸠书 2024-11-23 06:35:34

尝试在 void 上使用模板部分专业化

template <class F> class Instruction<F, void>
{
  protected:
    F (func);

  public:

   template <typename T>
   Instruction(T const& f)
   {
     func = &f;
   };

   template <typename A> void execute(A const& a)
   {
     (func)(a);
   };
   template <typename A> void execute(A const& a,A const& b)
   {
     (func)(a,b);
   };
   template <typename A> void execute(A const& a,A const& b,A const& c)
   {
     (func)(a,b,c);
   };
};

Try a template partial specialization on void:

template <class F> class Instruction<F, void>
{
  protected:
    F (func);

  public:

   template <typename T>
   Instruction(T const& f)
   {
     func = &f;
   };

   template <typename A> void execute(A const& a)
   {
     (func)(a);
   };
   template <typename A> void execute(A const& a,A const& b)
   {
     (func)(a,b);
   };
   template <typename A> void execute(A const& a,A const& b,A const& c)
   {
     (func)(a,b,c);
   };
};
稚气少女 2024-11-23 06:35:34

您可能需要使用 Boost: :FunctionTypes 它提供了一个能够分解函数类型并允许您访问返回或参数类型的元函数。

You may want to use Boost::FunctionTypes which provide a metafunction able to decompose function type and give you access to the return or argument type.

挽你眉间 2024-11-23 06:35:34

在 C++0x 中,您可以decltype。但是,在 C++03 中,按照惯例,函数对象具有 result_type typedef,并且函数指针可以应用模板推导。

In C++0x you can decltype. However, in C++03, by convention, function objects have a result_type typedef, and function pointers can have template deduction applied to them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文