Search code examples
c++c++17variadic-templatestemplate-meta-programmingfold-expression

Are fold expressions fully supported in VS2017?


I found an interesting article and tried its code with 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);
    }

, but the code did not compile with errors:

error C2760: syntax error: unexpected token '...', expected ';'
error C3520: 'Idx': parameter pack must be expanded in this context

does it mean that VS2017 does not fully support fold expressions?


Solution

  • This code needs just a couple of syntax fixes:

    (f(std::get<Idx>(t)), ...);
    

    and

    WriteV(out, a.n, a.s, a.d);
    

    Note that this code is unnecessary long for some reason. It can be replaced with just

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