Axe*_*ier 5 templates visual-studio variadic-templates c++11
考虑以下简化的 C++ 代码:
template <typename ... TEventArgs>
struct Event
{
// ...
};
template <typename T>
struct Parameter
{
using Type = T;
// ...
};
template <typename ... Parameters>
struct Command
{
Event<typename Parameters::Type...> Invoked;
};
int main()
{
Command<Parameter<int>, Parameter<float>> c;
}
Run Code Online (Sandbox Code Playgroud)
Visual Studio C++ 编译器(2013 年 11 月 CTP、Visual Studio 2013 Update 1)产生以下错误:source.cpp(17): error C3546: '...' : There are noparameter packs available to Expand
明格 4.8.1。另一方面编译代码没有任何问题。显然,Visual Studio 编译器有一个错误,当表达式涉及访问可变参数类型时,该错误会阻止它扩展参数包。不过,其他扩展也有效。例如,编译成功,或者您甚至可以成功访问静态成员以在的构造函数Event<std::vector<Parameters>...> Invoked;中调用可变参数函数: 。CommandSomeVariadicFunc(Parameters::SomeStaticFunc()...);
所以,问题是:
1)哪个编译器是错误的:Visual Studio还是mingw?尽管我没有看到任何会阻止typename Parameters::Type参数包扩展工作的内容,但我不能 100% 确定它是有效的 C++。
2)有解决办法吗?基本上,我必须执行从“序列”Parameters到“序列”的投影Parameters::Type。那可能吗?我尝试使用递归结构构建该列表,但我只能想出类似的东西myStruct<type1, mystruct<type2, mystruct<type3, ...>>>,这不是我需要的。
感谢您的帮助。
Yakk 能够在上面的评论中提出解决该问题的方法。与 Visual Studio 和 mingw 完美编译的最终版本如下:
template <typename ... TEventArgs>
struct Event
{
// ...
};
template <typename T>
struct Parameter
{
using Type = T;
// ...
};
template <typename ... Parameters>
struct Command
{
private:
// Workaround for the Visual Studio bug
template<typename T> struct ExpandArgs
{
typedef typename T::Type Type;
};
public:
Event<typename ExpandArgs<Parameters>::Type...> Invoked;
};
int main()
{
Command<Parameter<int>, Parameter<float>> c;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2972 次 |
| 最近记录: |