VS2017是否完全支持折叠表达式?

Ale*_*sky 1 c++ template-meta-programming variadic-templates fold-expression c++17

我找到了一篇有趣的文章并尝试了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);
    }
Run Code Online (Sandbox Code Playgroud)

,但代码没有编译错误:

error C2760: syntax error: unexpected token '...', expected ';'
error C3520: 'Idx': parameter pack must be expanded in this context
Run Code Online (Sandbox Code Playgroud)

这是否意味着VS2017不完全支持折叠表达式?

use*_*670 6

此代码只需要几个语法修复:

(f(std::get<Idx>(t)), ...);
Run Code Online (Sandbox Code Playgroud)

WriteV(out, a.n, a.s, a.d);
Run Code Online (Sandbox Code Playgroud)

请注意,由于某种原因,此代码是不必要的.它可以直接替换

 template<typename ... Args>
 void WriteV(std::wostream & out, Args const & ... args)
 {
     (out << ... << args);
 }
Run Code Online (Sandbox Code Playgroud)

  • ......和`void main()`... (2认同)