我正在尝试构建一个类模板,其中一个静态方法需要在模板参数中指定 typedef。 目标是指定一个像这样的 typedef
typedef foobar = void __stdcall foo(int a, float b)
并将其传递给我的模板 Foo<foobar>();
。由此看来,类 Foo 应该有一个具有确切 typedef 的静态成员函数 void __stdcall foo(int a, float b)
我的这个模板类的初始草案如下所示:
template<class T, class ... Args>
class Foo
{
static T Bar(Args... args);
};
其中
Bar
是从模板创建的静态方法。
这不考虑调用约定、隐式 this 指针等...但是我能够创建一个具有正确返回类型和参数的函数。是否可以从 typedef 创建函数?
是否可以从 typedef 创建函数?
是的,但语法
typedef foobar = void __stdcall foo(int a, float b)
不正确。正确的语法如下所示:
// note the use of "using" and also that "foo" is removed from here
using foobar = void __stdcall (int a, float b)
现在谈到更重要的问题,函数的返回类型永远不能是函数类型,因此即使我们能够 typedef 函数类型,它也不能用作静态函数的返回类型。