::std::function 的 constexpr 版本

4 c++ metaprogramming template-meta-programming constexpr c++11

我正在寻找可在 constexpr 中使用的 ::std::function。用例:我有一个函数,它将函数指针作为参数,第二个函数将 lambda 传递给第一个函数。两者在编译时都是完全可执行的,所以我想对它们进行 constexpr。例如:

template <class _Type>
class ConstexprFunctionPtr
{
    private:
        using Type = typename ::std::decay<_Type>::type;
        const Type function;

    public:
        constexpr inline
        ConstexprFunctionPtr(const Type f)
        : function(f)
        { }

        template <typename... Types>
        constexpr inline
        auto
        operator() (Types... args)
        const {
            return function(args... );
        }
};

constexpr inline
void
test()
{
    ConstexprFunctionPtr<int(int)> test([](int i) -> int {
        return i + 1;
    });
    int i = test(100);

    ConstexprFunctionPtr<int(int)> test2([=](int i) -> int {
        return i + 1;
    });
    i = test2(1000);
}
Run Code Online (Sandbox Code Playgroud)

但是,这只有效,因为我将 lambda 转换为函数指针,当然,如第二个示例所示,捕获 lambda 失败。谁能给我一些关于如何通过捕获 lambda 来做到这一点的指示?

这将演示用例:

constexpr
void
walkOverObjects(ObjectList d, ConstexprFunctionPtr<void(Object)> fun) {
// for i in d, execute fun
}

constexpr
void
searchObjectX(ObjectList d) {
walkOverObjects(d, /*lambda that searches X*/);
}
Run Code Online (Sandbox Code Playgroud)

谢谢,杰克

更新:感谢您指出 C++20 解决方案,但是,我想要一个在 C++14 下工作的解决方案

Mat*_*ano 8

因此,C++20 发生了很多变化——最重要的是,您现在可以在constexpr上下文中使用动态内存和虚拟函数。这使得完全有可能构建 std::function 的 constexpr 版本。这是一个概念验证(它很长并且没有复制或移动构造函数,所以请不要按原样使用它)。它在 clang 10 下编译,在此处运行代码。我没有在其他编译器下尝试过,值得注意的是,目前没有一个主要编译器声称拥有 C++-20 的完整实现。

#include <type_traits>
#include <utility>
#include <functional>

template<typename Ret, typename... Args> struct _function{
    constexpr virtual Ret operator()(Args...) const = 0;
    constexpr virtual ~_function() = default;
};

template<typename F, typename Ret, typename... Args> struct _function_impl : public _function<Ret,Args...>{
    F f;
    constexpr Ret operator()(Args... args) const override {
        return f(std::forward<Args>(args)...);
    }
    constexpr _function_impl(F&& f):f(f){}
};

template<typename > struct function;

template<typename Ret, typename... Args> struct function<Ret (Args...)>{
    _function<Ret,Args...> *real_f{nullptr};
    constexpr Ret operator()(Args... args) const {
        return real_f->operator()(std::forward<Args>(args)...);
    }

    constexpr ~function(){
        if (real_f) delete real_f;
    }

    template<typename F>
    constexpr function(F&& f):real_f(new _function_impl<std::decay_t<F>,Ret,Args...>(std::move(f))){}

};

template<typename Ret, typename... Args>
constexpr Ret call_f_2(const function<Ret(Args...)> &f, Args... a){
    return f(std::forward<Args>(a)...);
}

template<typename F, typename... Args>
constexpr decltype(auto) call_f(F && f, Args&&... a){
    using Ret = std::invoke_result_t<std::decay_t<F>,Args...>;
    function<Ret(Args...)> f2 = std::move(f);
    return call_f_2<Ret,Args...>(f2,a...);
}

int main(){
    constexpr int c = 3;
    constexpr int i = call_f([c](int j) constexpr {return c + j;},4);
    return i;
}
Run Code Online (Sandbox Code Playgroud)

  • 好的!您可能想澄清您的问题以指出这一点 - 现在它没有提到标准并标记为“C++-17” (2认同)

Dav*_*aim 5

我正在寻找可在 constexpr 中使用的 ::std::function

停在这里。不可能。std::function是一个多态包装函数。无状态 lambdas、有状态 lambdas、函子、函数指针、函数引用——所有这些都可以构建一个std::function可以在运行时更改的有效值。所以使编译时间等效只是浪费时间。

如果你只想要一个编译时通用函数参数,你可以只使用模板

template<class functor_type>
class my_generic_function_consumer_class{

   using decayed_function_type = typename std::decay_t<functor_type>;

   decayed_function_type m_functor;

};
Run Code Online (Sandbox Code Playgroud)

在您有问题的代码中,只需接受一个通用函子,并使用static_assert以下方法验证它:

template<class function_type>
constexpr void walkOverObjects(ObjectList d, function_type&& fun) {
    static_assert(std::is_constructible_v<std::function<void(ObjectList), function_type>>,
                  "function_type given to walkOverObjects is invalid.");
}
Run Code Online (Sandbox Code Playgroud)