How to emulate the expression of a bend in a variational function?

Microsoft seems to be slowly supporting Fold Expressions, and I'm struggling to find a way to emulate it. I have the following reduction example. This does not compile in Visual Studio 2017 due to the lack of support for Fold Expressions. Any idea how to emulate it?

template<typename... Args>
void get(weak_ptr<sqlite::database> awdb_, Args&&... args) {
    (*awdb_.lock() << ... << args);
}
+4
source share
1 answer

Before the smoothing expressions there was an expander trick :

template <class... Args>
void get(weak_ptr<sqlite::database> awdb_, Args&&... args) {
    if (auto db = awdb_.lock()) {
        using expander = int[];
        (void)expander{0,
            (void(*db << std::forward<Args>(args)), 0)...
        };
    }
}

As TC points out, this is not necessarily strictly equivalent if it <<does something other than backtracking *this, so we could write:

template <class F, class Arg>
decltype(auto) fold(F&& , Arg&& arg) {
    return std::forward<Arg>(arg);
}

template <class F, class A0, class A1, class... Args>
decltype(auto) fold(F&& f, A0&& a0, A1&& a1, Args&&... args) {
    return fold(f, f(a0, a1), std::forward<Args>(args)...);
}

template <class... Args>
void get(weak_ptr<sqlite::database> awdb_, Args&&... args) {
    if (auto db = awdb_.lock()) {
        auto lshift = [](auto&& a, auto&& b) -> decltype(auto) { return a << std::forward<decltype(b)>(b); };
        fold(lshift, *db, std::forward<Args>(args)...);
    }
}    
+6
source

Source: https://habr.com/ru/post/1669626/


All Articles