回路参数包lambda

问题描述 投票:0回答:1

我有少量代码。

我想知道是否可以切出ProcessArg函数

我想从调用函数内部循环参数包。

我最好在initializer_list或其他东西中使用lambda函数,如果是的话,我该怎么做。

谢谢

template <class R, class Arg>
R ProcessArg(Arg&& arg) {
    std::cout << typeid(arg).name() << (std::is_reference<Arg>::value ? "&" : "") << std::endl;
    //std::cout << std::boolalpha << std::is_reference<Arg>::value << std::endl; // not always true
    return R();
}

template <typename R, typename... Args>
R CallFunction(Args&&... args) {
    std::size_t size = sizeof...(Args);
    std::initializer_list<R>{ProcessArg<R>(std::forward<Args>(args)) ...};
    return R();
}

template<typename Fn> class FunctionBase;
template<typename R, typename... Args>
class FunctionBase <R(*)(Args...)> {
public:
    FunctionBase() {}
    R operator()(Args&&... args) { // Args&& is a universal reference
        return CallFunction<R>(std::forward<Args>(args)...);
    }
};

int foo(int a, int& b) {
    std::cout << std::boolalpha << std::is_reference<decltype(a)>::value << std::endl; // falae
    std::cout << std::boolalpha << std::is_reference<decltype(b)>::value << std::endl; // true
    return a + b;
}

int main() {
    int in = 10;
    foo(1, in);

    FunctionBase<decltype(&foo)> func;
    func(1, in);
}
c++ c++11 c++17
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.