How can I determine whether a function's parameter type is a function? I'm implementing a class called Queue
which receives a single parameter. If the parameter is a function, it stores the function.
Here is the code:
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);
// }
}
}
}
It can be initialised:
Queue queue1 = new Queue <int> (func ()); // func () is a function.
Queue queue2 = new Queue <int> (var); // var is a variable.
For a SFINAE example:
(You need to include
<type_traits>
to usestd::is_function_v
)Oh my. This is a bit of an XY problem.
Anyway, after messing around with
std::enable_if
for a bit (which was kinda fun), I realised that the whole thing can be boiled down to this:And, if I understand the OP's problem right, that is all all you need.
Test program:
Output:
Live demo.
Moral: KISS, there are far too many toys in the toybox these days. Enjoy the weekend people.
And, since I took the time to research it a bit (mainly because I wanted to learn a bit about it), here is a bit of super-simple SFINAE cobbled together from the wise ones.
Output:
Live demo.
Powerful stuff, but not the answer to every little problem.
You can use the
std::is_function
to do that.An implementation like
won't work though, since the part inside the
if
block scope is seen for other data types by the compiler.To realize that you'll need to use SFINAE, and provide different specializations of your
Queue
constructor function usingstd::enable_if
.