VS2013 fails with variadic template specialization

浪尽此生 提交于 2019-12-23 13:16:36

问题


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;

But the new shiny VS2013 compiler bails out with

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

Which one is at fault?


回答1:


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;

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.




回答2:


That's not a bug (in VC++) there is a spare template parameter after the variadic list. This is not valid C++ 11.

Looks like a bug in GCC and CLang.



来源:https://stackoverflow.com/questions/17332749/vs2013-fails-with-variadic-template-specialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!