本文介绍了VS2013失败,可变模板专门化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码适用于g ++和Clang:

This piece of code works fine with g++ and Clang:

template <typename Sig, Sig& S> struct OpF;

template <typename TR, typename ... Ts, TR (&f)(Ts...)>
struct OpF<TR (Ts...), f> {
};

int foo(int x) {
  return 0;
}

OpF<int (int), foo> f;

但是新的闪亮VS2013编译器提供了

But the new shiny VS2013 compiler bails out with

f.cpp(4) : error C3520: 'Ts' : parameter pack must be expanded in this context

哪一个是错误的?

推荐答案

VS2013编译器,这似乎已经修复了。有关解决方法,请参阅:。然而,你的代码可能仍然不会编译,由于VS2013编译器中的另一个错误,它导致它解析模板参数 TR(& f)(Ts ...) TR(& f)(void)。虽然,也有一个解决方法。您可以使用嵌套类,然后可以使用参数包。例如:

This was a bug in the VS2013 compiler, which seems to have been fixed now. For a workaround, see: Visual C++ 12 (VS2013 Preview) variadic template with function parameter workaround. However, your code probably still won't compile, due to another bug in the VS2013 compiler, which causes it to parse the template argument TR (&f)(Ts...) as TR (&f)(void). Though, there is a workaround for that too. You can use a nested class, which then can use the parameter pack. For example:

template <typename Sig> struct OpF;

template <typename TR, typename ... Ts>
struct OpF<TR (Ts...)> {
  template <TR (&f)(Ts...)>
  struct Fn {
  };
};

int foo(int x) {
  return 0;
}

OpF<int (int)>::Fn<foo> f;

在模板声明中不允许参数包之后的模板参数。但是,Clang和GCC确实允许在模板专门化 。所以可能是Clang和GCC对于C ++ 11规范太自由了。

Template parameters after a parameter pack are not allowed in template declarations. However, Clang and GCC do allow it in a template specialization. So it could be that Clang and GCC are being too liberal with the C++11 specs.

这篇关于VS2013失败,可变模板专门化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 06:36