gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:36:50 26 4
gpt4 key购买 nike

如何判断一个函数的参数类型是否是一个函数?我正在实现一个名为 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com