c++ - 判断函数参数是否为函数

标签 c++ templates

如何判断一个函数的参数类型是否是一个函数?我正在实现一个名为 Queue 的类,它接收一个参数。如果参数是函数,则存储函数。

代码如下:

template <class Type, typename Data>
class Queue {
    public:
        void Enqueue (Data& data) {
            if (typeid(data).name() == int) {
                intVector.push_back(data);
                order.push_back("int");
            }
            else if (typeid(data).name() == bool) {
                boolVector.push_back(data);
                order.push_back("bool");
            }
            else if (typeid().name() == string) {  
              stringVector.push_back(data);
              order.push_back("string");
            }
            // This will continue for:
            // - double
            // - char
            // - function
        }

        auto Dequeue () {
            auto temp;
            switch (order.begin()) {
                case "int":
                    temp = intVector.begin();
                    intVector.erase(intVector.begin());
                    order.erase(order.begin());
                    return temp;
                // This will continue for:
                // - "string"
                // - "bool"
                // - "char"
                // - "double"
                // - "function"
                default:
                    cout << "An Error occurred while trying to Enqueue." << endl;
                    cout << "\tAddress: " << this << endl;
            }
        }

        auto Start () {
            // This function will run all of the processes...
        }
        Queue (Data& data) {
            if (typeid(Type).name() == int) {
                // Pseodo-code:
                // if (data.type == function) {
                //     Enqueue (data);
                // }
            }
        }
}

可以初始化:

Queue queue1 = new Queue <int> (func ()); // func () is a function.
Queue queue2 = new Queue <int> (var);     // var is a variable.

最佳答案

天哪。这有点 XY problem .

无论如何,在弄乱了 std::enable_if 之后(这很有趣),我意识到整个事情可以归结为:

#include <vector>
#include <string>
#include <any>
#include <iostream>
#include <functional>

void call_if_function (void (* f) ()) { f (); }
void call_if_function (std::function <void ()> f) { f (); }
void call_if_function (std::any x) { (void) x; }

template <class T>
class Queue
{
    public:
        void Enqueue (const T& data)
        {
//          std::cout << "Enqueueing " << data << "\n";
            v.push_back (data);
        }

        T Dequeue ()
        {
            T ret = v.front ();
//          std::cout << "Dequeueing " << ret << "\n";
            v.erase (v.begin ());
            call_if_function (ret);
            return ret;
        }

    private:
        std::vector <T> v;
};

而且,如果我正确理解 OP 的问题,这就是您所需要的。

测试程序:

void foo () { std::cout << "foo () called\n"; }
void bar (int x, int y) { std::cout << "bar () called, x = " << x << ", y = " << y << "\n"; }

int main ()
{
    // Queue of int's
    Queue <int> int_q;
    int_q.Enqueue (42);
    auto i = int_q.Dequeue ();
    std::cout << "int_q.Dequeue () returned " << i << "\n\n";

    // Queue of strings
    Queue <std::string> string_q;
    string_q.Enqueue ("Hello world");
    auto s = string_q.Dequeue ();
    std::cout << "string_q.Dequeue () returned " << s << "\n\n";

    // Call function with no parameters    
    Queue <void (*)()> func_q;
    func_q.Enqueue (foo);
    auto f = func_q.Dequeue ();
    std::cout << "func_q.Dequeue () returned " << (void *) f << "\n";
    f ();

    // Call function with arbitrary parameters
    Queue <std::function <void ()>> func_qp;
    func_qp.Enqueue ([] () { bar (21, 99); });
    auto fp = func_qp.Dequeue ();
    fp ();
}

输出:

int_q.Dequeue () returned 42

string_q.Dequeue () returned Hello world

foo () called
func_q.Dequeue () returned 0x4026fd
foo () called

bar () called, x = 21, y = 99
bar () called, x = 21, y = 99

Live demo .

寓意:KISS,现在玩具箱里的玩具太多了。享受周末的人们。

关于c++ - 判断函数参数是否为函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51112395/

相关文章:

在 Visual Studio 2005 上使用 TCHAR 的 C++ 模板函数特化

c++ - 使用通用模板函数为 std::ostream 和 std::vector 专门化 operator<< 的最佳方法?

c++ - 我怎样才能得到一个数组的大小,所以我可以从一个函数声明所述数组

c++ - 如何可移植(反)序列化 qint32?

c++ - 如何使子控件处理父 CView 的加速器命令

c++ - Halide::GPU 上的缓冲区

c++ - 在编译时计算 C 字符串的长度。这真的是一个 constexpr 吗?

c++ - 依赖范围;前面需要typename;

C++ 模板 : How to conditionally compile different code based on data type?

C++ 参数应按值传递,但编译器将其视为引用