在我的C++ 11代码中,我有一个可变结构和一个函数,它应该对struct的可变参数类型使用完美转发,例如:
template <typename... T>
struct S
{
void X(T&&... args)
{
Do(std::forward<T>(args)...);
}
};
Run Code Online (Sandbox Code Playgroud)
假设这Do是一个独立的可变参数函数.鉴于struct V { int x, y; };我想要这样调用的类型S::X:
S<V> s;
V v = { 1, 2 };
s.X(V()); // Compiles
s.X(v); // Does not compile
Run Code Online (Sandbox Code Playgroud)
最后一行在Visual Studio 2013和Visual Studio 2013中使用C++编译器的2013年11月CTP产生以下错误:
error C2664: 'void S<V>::X(V &&)' : cannot convert argument 1 from 'V' to 'V &&'
You cannot bind an lvalue to an rvalue reference
Run Code Online (Sandbox Code Playgroud)
我尝试了mingw 4.8.1并得到了类似的错误,因此它似乎不是编译器或C++ 11支持问题:
Source.cpp:51:7: error: cannot …Run Code Online (Sandbox Code Playgroud) 考虑以下简化的 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?尽管我没有看到任何会阻止 …