C ++ 11中的通用函数指针 [英] Generic function pointer in C++11

查看:193
本文介绍了C ++ 11中的通用函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个方法执行队列在C ++ x0。我已经实现并验证了基本的队列机制,但是希望使用 push()自动删除对特定方法的所有调用的选项修改它:

I am currently in the process of writing a method execution queue in C++x0. I have implemented and verified the basic queue mechanism but want to amend it with an option to have push() automatically remove all previous calls to a specific method:

queue.push(this, &Obj::foo, 1);
queue.push(this, &Obj::foo, 2);
queue.push(this, &Obj::foo, 3);

应与只呼叫

queue.push(this, &Obj::foo, 3);

我的代码到目前为止:

Queue.h:

#pragma once

#include <functional>
#include <vector>

using std::vector;
using std::function;
using std::bind;

class Queue
{

    private:
        struct functioncall {
            std::function<void()> call;
        };

        vector<functioncall> queue;

    public:
        Queue();
        ~Queue();

        template<typename T, typename F, typename... Args>
        int push(T, F , Args... args);

        int pop();
        bool empty();
        size_t size();
};


template<typename T, typename F, typename... Args>
int Queue::push(T instance, F func, Args... args)
{
    functioncall newelem = { bind(func, instance, args...) };
    queue.push_back(newelem);
    return queue.size();
}

Queue.cpp:

Queue.cpp:

#include "Queue.h"

Queue::Queue() : queue()
{
}

Queue::~Queue()
{
    delete &queue;
}

int Queue::pop()
{
    if(!queue.empty())
    {
        queue.front().call();
        queue.erase(queue.begin());
        return queue.size();
    }
    return 0;
}

bool Queue::empty()
{
    return queue.empty();
}

size_t Queue::size()
{
    return queue.size();
}



我已经准备好了 queue 以一个结构体,我想不仅保存 std :: bind 的结果,而且还指向被调用的方法的指针,所以我可以查找

I have already prepared the vector queue to take a struct in wich I want to not only save the result of std::bind but also the pointer to the method being called so I can look for that pointer and remove the old entries.

问题是传递给 push()的函数可以任意数量的参数。是否有一个通用的指针类型(它不必是可执行的,只要是相同的,当我反复推相同的函数到队列)可以这样做?

The issue is that the functions passed to push() can take an arbitrary amount of arguments. Is there a generic pointer type (it doesn't have to be executable, just be the same when I repeatedly push the same function to the queue) that can do that?

推荐答案

每个5.2.10p10,你可以转换一个指针到成员函数 T :: *(A1,A2,...)到另一个指向成员函数类型 U :: *(B1,...)的指针,并且不丢失信息; std :: less 可以将指针与成员函数进行比较,因此通过转换为一个虚指针成员类型 void(Impl :: *)()

Per 5.2.10p10, you can cast a pointer to member function T::*(A1, A2, ...) to another pointer to member function type U::*(B1, ...) and back with no loss of information; std::less can compare pointers to member functions so by casting to a dummy pointer-to-member type void (Impl::*)() you can compare pointer to member functions with the same signature.

但是,不能保证指向具有不同签名的成员函数的指针将比较不同的时候转换成相同的指针到成员类型,所以你将需要编码的签名在你的可比类型。 typeid 将在这里工作:

However, it is not guaranteed that pointer to member functions with different signatures will compare different when cast to the same pointer to member type, so you will need to encode the signature in your comparable type. typeid will work here:

auto key = std::make_pair(&typeid(F), reinterpret_cast<void (Queue::*)()>(func));

这假设 F 到成员函数;如果用户尝试传递一些其他可调用对象,则这将中断。

This assumes that F is indeed a pointer to member function; if the user attempts to pass some other callable object then this will break.

这篇关于C ++ 11中的通用函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆