参数包扩展失败 [英] Parameter pack expansion fails

查看:100
本文介绍了参数包扩展失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下简化的C ++代码:

Consider the following simplified C++ code:

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;
}

Visual Studio C ++编译器(2013年11月CTP,Visual Studio 2013 Update 1)产生以下错误:
source.cpp(17):错误C3546:'...':没有参数包可扩展

The Visual Studio C++ compiler (November 2013 CTP, Visual Studio 2013 Update 1) produces the following error: source.cpp(17): error C3546: '...' : there are no parameter packs available to expand

Mingw 4.8 .1。另一方面,编译代码没有任何问题。显然,Visual Studio编译器存在一个错误,当表达式涉及访问可变参数的类型时,它会阻止其扩展参数包。不过,其他扩展也可以。例如, Event< std :: vector< Parameters> ...>调用; 成功编译,或者您甚至可以成功地访问静态成员以在 Command 的构造函数中调用可变参数函数,如下所示: SomeVariadicFunc(Parameters :: SomeStaticFunc()...);

Mingw 4.8.1. on the other hand compiles the code without any problems. Apparently, the Visual Studio compiler has a bug that prevents it from expanding the parameter pack when the expression involves accessing a type of the variadic parameters. Other expansions work, though. For instance, Event<std::vector<Parameters>...> Invoked; compiles successfully or you could even successfully access static members to call a variadic function like this in Command's constructor: SomeVariadicFunc(Parameters::SomeStaticFunc()...);.

因此,问题是:

1)哪个编译器错误:Visual Studio或mingw?尽管我看不到任何会阻止 typename Parameters :: Type 参数包扩展正常工作的东西,但我不是100%地确定它是有效的C ++。

1) Which compiler is wrong: Visual Studio or mingw? Although I don't see anything that would prevent the typename Parameters::Type parameter pack expansion from working, I'm not 100% sure it's valid C++.

2)是否有解决方法?基本上,我必须执行从 Parameters 的序列到 Parameters :: Type 。那可能吗?我试图使用递归结构构造该列表,但我只能想到类似 myStruct< type1,mystruct< type2,mystruct< type3,...>>> ,这不是我所需要的。

2) Is there a work around? Basically, I would have to perform a projection from a "sequence" of Parameters to a "sequence" of Parameters::Type. Is that possible? I tried to construct that list using a recursive struct but I could only come up with something like myStruct<type1, mystruct<type2, mystruct<type3, ...>>>, which is not what I need.

谢谢您的帮助。

推荐答案

Yakk在上面的评论中提出了解决该问题的方法。可以与Visual Studio和mingw完美编译的最终版本如下:

Yakk was able to come up with a workaround for the problem in the comments above. The final version that compiles perfectly with both Visual Studio an mingw is the following:

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;
}

这篇关于参数包扩展失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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