如何传递构造函数(可变参数)作为模板参数

how to pass construction function(variadic arguments) as template parameter?

本文关键字:参数 变参 何传递 构造函数      更新时间:2023-10-16

C++的可变模板功能强大,但很难编写这样的代码。我的问题来了:如何通过模板传递Class的构造(见下面的代码片段)?

注意:因为我想得到一个通用的解决方案,所以构造的参数必须是可变的。此外,我想设置每个参数的默认值

有人能帮我吗?

#include <iostream>
#include <utility>
template< typename R, typename C, typename... Args>
class delegate
{
public:
    template<R(C::*F)(Args...)>
    struct adapter 
    {
        static R invoke_no_fwd(Args... args) 
        { 
            C t; // how to pass the construction function of C through template??? and set default value for each argument
            return (t.*F)(args...); 
        }
    };
};
class Class 
{
public:
    Class(int param)
        : m_val(param)
    {}
    void print(int v) 
    {
        std::cout << "Class: " << v + m_val << std::endl;
    }
private:
    int m_val;
};
int main(int argc, char** argv) 
{
    using namespace std;
    // because the below code doesn't contain construction info, so it won't compile
    typedef void(*function_t)(int);
    function_t ptrFunc = (delegate<void, Class, int>::adapter<&Class::print>::invoke_no_fwd);
    auto type = (delegate<void, Class, int>::adapter<&Class::print>::invoke_no_fwd);
    cout << typeid(type).name() << endl;
    return 0;
}

您可以使用std::integral_constant<typename T, T>:执行以下操作

template< typename R, typename C, typename... Args>
class delegate
{
public:
    template<R(C::*F)(Args...), typename ... Ts>
    struct adapter {
        static R invoke_no_fwd(Args... args) {
            C t((Ts::value)...);
            return (t.*F)(args...);
        }
    };
};

并像一样使用它

int main()
{
    //using namespace std;
    typedef void(*function_t)(int);
    function_t ptrFunc = (delegate<void, Class, int>::adapter<&Class::print, std::integral_constant<int, 42> >::invoke_no_fwd);
    auto type = (delegate<void, Class, int>::adapter<&Class::print, std::integral_constant<int, 42>>::invoke_no_fwd);
    ptrFunc(-42);
    type(0);
    return 0;
}

活生生的例子。

分配程序使用宏来调用析构函数。

template<class _Ty> inline
    void _Destroy(_Ty _FARQ *_Ptr)
    {   // destroy object at _Ptr
    _DESTRUCTOR(_Ty, _Ptr);
    }
#define _DESTRUCTOR(ty, ptr)    (ptr)->~ty()

这能解决你的问题吗?