我发现了一篇有趣的文章,并用 MSVS 2017 尝试了它的代码:
#include <utility>
#include <tuple>
template <typename... Args, typename Func, std::size_t... Idx>
void for_each(const std::tuple<Args...>& t, Func&& f, std::index_sequence<Idx...>) {
f(std::get<Idx>(t))...;
}
template <typename... Args, typename Func>
void for_each(const std::tuple<Args...>& t, Func&& f) {
for_each(t, f, std::index_sequence_for<Args...>{});
}
template <typename T>
void Write(std::wostream & out, const T & t)
{
out << t;
}
template<typename ...Args>
void WriteV(std::wostream & out, Args&... args)
{
for_each(std::tuple<Args&...>(args...), [&out](auto& a) { Write(out, a); });
}
struct A
{
int n;
std::wstring s;
double d;
};
void main()
{
std::wostringstream out;
A a{ 1, std::wstring(L"2"), 3.0 };
WriteV(a.n, a.s, a.d);
}
,但代码没有编译错误:
error C2760: syntax error: unexpected token '...', expected ';'
error C3520: 'Idx': parameter pack must be expanded in this context
这是否意味着VS2017不完全支持折叠表达式?