Variadic templates lambda in c++

Viewed 83

I'm trying to implement conversion from ptmf to function pointer. Code below works well in clang and gcc compiler, but not in MSVC.

Am I doing something wrong or it is a MSVC bug?

#include <iostream>

template <auto fn>
inline constexpr auto as_static_function() {
    return []<typename T, typename R, typename... Args>
        (auto(T::*)(Args...) -> R) -> auto(*)(T*, Args...) -> R {
            return [](T* self, Args... args) -> R {
                return (self->*fn)(args...);
            };
        }(fn);
}


struct Test {
    void func() {
        std::cout << 20 << std::endl;
    }
};

int main() {
    Test test{};
    auto fn = as_static_function<&Test::func>();
    fn(&test);
    return 0;
}

MSVC gives errors

main.cpp(10): error C3520: 'Args': parameter pack must be expanded in this context
main.cpp(22): note: see reference to function template instantiation 'auto as_static_function<void Test::func(void)>(void)' being compiled
main.cpp(8): error C2065: 'fn': undeclared identifier

But code below works well in MSVC

template<auto fn, typename Self, typename R, typename... Args>
inline constexpr auto as_static_function(auto(Self::*)(Args...) -> R) -> auto(*)(Self*, Args...) -> R {
    return [](Self* self, Args... args) -> R {
        return (self->*fn)(args...);
    };
}

template <auto fn>
inline constexpr auto as_static_function() {
    return as_static_function<fn>(fn);
}
0 Answers
Related