Visual C ++ 12(VS2013预览)可变参数模板函数参数解决方法 [英] Visual C++ 12 (VS2013 Preview) variadic template with function parameter workaround

查看:2686
本文介绍了Visual C ++ 12(VS2013预览)可变参数模板函数参数解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚提交了 Microsoft Connect 关于无法编译以下代码的玩具代码片段:

  template< typename ... P& struct S {
template< void(* F)(P ...)> static void T(){}
};

void A(int,float){}
int main(){S< int,float> :: T& A& }

错误是:

  test.cpp(2):error C3520:'P':参数包必须在此上下文中扩展

基本上,当用作模板参数时,我无法解开函数签名中的可变类型。这个代码是(我认为)法律;至少,GCC 4.7,Clang 3.0和ICC 13都支持它。



我看过这个SO问题,但没有请求或给出解决方法,这正是我正在寻找的。

虽然不是超级关键的(显然我已经得到了没有可变参数模板多年了)这种模式对我想做的一些工作和一些文章我想做的特别重要关于C ++ 11反射技术的序列化,脚本绑定等,这是我想要有用的Visual Studio用户(因为它是迄今为止主流的编译器工具集在我的行业)。我希望微软的工程师能够在2013年RTM解决这个问题,但我没有喘口气。



一个玩具)示例如何使用它是类似的(减去宏使它稍微更容易使用):

 反射< MyType>(MyType)
.bind(GetMatrix,mat44,& MyType :: GetMatrix>()
.bind(Display,void,& MyType :: Display> );

当然,这一切都可以在没有可变参数模板的情况下完成,当然,接受绑定成员函数的最大arity的限制,是的,传递函数作为模板参数是导入,由于成员函数指针在Visual Studio中工作的性质(可变大小)和希望实现效率与不可能快的C ++代理(在我的C ++社区的利基,这个优化级别有时候实际上很重要),否定使用 std :: function 或类似设计的选项。



这个VS bug有解决方法吗?或者任何其他方式使用可变参数模板在VC ++ 12中使用(编译时)函数指针参数?

解决方案

不确定为什么,但简化与typedef似乎工作:

 模板< typename ... P& 
struct S
{
typedef void(* MyFunc)(P ...);

template< MyFunc myFunc>
static void foo(){}
};

void foo2(int,float){}

int main()
{
S< int,float> :: foo& foo2();
}

至少在Visual Studio 2013 Ultimate Preview。


I just filed this bug on Microsoft Connect regarding the inability to compile the following toy snippet of code:

template <typename... P> struct S {
    template <void(*F)(P...)> static void T() { }
};

void A(int, float) { }
int main() { S<int, float>::T<&A>(); }

The error is:

test.cpp(2): error C3520: 'P' : parameter pack must be expanded in this context

Essentially, I cannot unpack a variadic type inside of a function signature when used as a template parameter. This code is (I think) legal; at least, GCC 4.7, Clang 3.0, and ICC 13 all support it.

I've seen this SO question but no workarounds are requested or given, which is what I'm looking for.

While not super critical (obviously I've been getting by without variadic templates for many years now) this pattern is of particular importance to some of the work I'd like to do and some articles I'd like to do on C++11 reflection techniques for serialization, script binding, etc. which is something I'd like to be useful to Visual Studio users (as it is by far the dominant compiler toolset in my industry). I'd like to hope that Microsoft's engineers will be able to fix this by 2013 RTM, but I'm not holding my breath.

A toy (from memory this time) sample of how this is being used is something like (minus the macros that make it slightly easier to use):

Reflect<MyType>("MyType")
.bind("GetMatrix", mat44, &MyType::GetMatrix>()
.bind("Display", void, &MyType::Display>();

Of course this can all be done without variadic templates. It just takes large masses of code and accepting limitations to the maximum arity of bound member functions, of course. And yes, passing the functions as a template parameter is of import, due to the nature of how member function pointers work in Visual Studio (variable size) and a desire to achieve efficiency on par with Impossibly Fast C++ Delegates (in my niche of the C++ community, this level of optimization can sometimes actually matter) which negates the option of using std::function or similar designs.

Is there a work-around for this VS bug? Or any other way to use variadic templates to use a (compile-time) function pointer parameter in VC++12?

解决方案

Not sure why but simplifying with a typedef seems to work:

template <typename... P>
struct S
{
    typedef void (*MyFunc)(P...);

    template <MyFunc myFunc>
    static void foo() {}
};

void foo2(int, float) {}

int main()
{
    S<int, float>::foo<&foo2>();
}

At least on the Visual Studio 2013 Ultimate Preview.

这篇关于Visual C ++ 12(VS2013预览)可变参数模板函数参数解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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