【问题标题】:how to pass construction function(variadic arguments) as template parameter?如何将构造函数(可变参数)作为模板参数传递?
【发布时间】:2014-08-25 07:03:19
【问题描述】:

C++ 的可变参数模板很强大,但是很难写出这样的代码。我的问题来了:如何通过模板传递Class(见下面的代码sn-p)的构造?

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

谁能帮帮我?

#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::function ?你愿意接受,还是真的想自己重写所有内容?
  • @quantdev 不,我想编写一个适配器,可以将非静态成员函数转换为 c 风格的函数指针。我的目的是让开发 MFC 更方便。现有的 API 接口无法更改。这是微软的秘密
  • 但是std::function也可以转换成C风格的函数ptr...
  • @quantdev,绝对不是。看看这个:stackoverflow.com/questions/18370396/…
  • 你说的是 c 风格的可变参数函数,然后是 printf

标签: c++ visual-studio templates design-patterns


【解决方案1】:

您可以使用std::integral_constant&lt;typename T, T&gt; 执行以下操作:

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;
}

Live example.

【讨论】:

  • Visual Studio 2013:1 中存在编译错误>Console.cpp(12): error C3520: 'Ts' : parameter pack must be expand in this context 1> Console.cpp(11) :编译类模板成员函数 'void delegate::adapter<:print>>::invoke_no_fwd(int)' 1> Console.cpp(37) : 参见正在编译的函数模板实例化 'void delegate::adapter<:print>>::invoke_no_fwd(int)' 1> Console.cpp (37) : 参见类模板实例化的参考
  • @Triumphant:在Ts::value 周围添加额外的父母,修复VS2013 的编译。已编辑
  • 它有效,您是模板编程专家。非常感谢
  • 另一个问题,我的构造函数想要 'const char*' 参数怎么样,看我的新问题:stackoverflow.com/questions/25580294/…,你能通过引用这个问题重写你的答案吗:stackoverflow.com/questions/25598265/…
【解决方案2】:

allocor 使用宏调用析构函数。

template<class _Ty> inline
    void _Destroy(_Ty _FARQ *_Ptr)
    {   // destroy object at _Ptr
    _DESTRUCTOR(_Ty, _Ptr);
    }

#define _DESTRUCTOR(ty, ptr)    (ptr)->~ty()

这能解决你的问题吗?

【讨论】:

  • 嗯...我关心的是构造函数,而不是析构函数。不管怎样,还是谢谢你
  • @Triumphant,我的意思是你可以尝试定义一个宏来调用构造函数。
猜你喜欢
  • 1970-01-01
  • 2014-10-20
  • 2013-05-25
  • 2019-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-18
  • 2018-02-03
相关资源
最近更新 更多