Permit me to say that I know this question does not fit into most conventions (if any at all) but out of curiosity and love for the programming language (C++) I'll ask this question anyway. Feel free to correct me with your answers or comments below.
The Question:
"Can we make variadic functions in C++ that accept arguments of multiple (and possibly previously unknown) data types and how would it be implemented?"
Example:
JavaScript Sample
function buildArgs(... args) {
let iterator = 0,
length = args.length;
for (iterator; iterator != length; iterator += 1) {
let arg = args[iterator];
build(arg)
}
}
buildArgs(1); // Valid
buildArgs(1, "Hello"); // Valid
buildArgs(1, "Hello", null) // Valid
(Hypothetical) C++ Sample:
template <class... Args, typename data>
inline void buildArgs(Args... args) {
int iterator = 0,
length = sizeof args;
for (iterator; iterator != length; iterator += 1) {
data arg = args[iterator];
build(arg);
}
}
buildArgs(1); // Valid
buildArgs(1, "Hello"); // Valid
buildArgs(1, "Hello", NULL); // Valid
From the examples given, the arguments considered valid to the function buildArgs
can be of any data type (char
, int
, std::string
and so on) while the function buildArgs
can accept any number of those valid arguments.
I've already done some minor research on variadic functions and templates in C++ but nothing I've seen has answered this question yet.
Again, I can not speak for the practicality of this feature but I would love to very much see if it is possible.
Links:
• Variadic Functions with Different Types of Arguments in C: variadic functions with different types of arguments in c
• Accept all Types as Argument in Function: Accept all types as argument in function
• C Programming : https://en.wikibooks.org/wiki/C_Programming/stdarg.h
• C++ Reference - Parameter Pack: https://en.cppreference.com/w/cpp/language/parameter_pack
• C++ Reference - Fold Expressions: https://en.cppreference.com/w/cpp/language/fold
• Variable Number of Arguments in C: Variable number of arguments in C++?
Conclusion:
Thank you for taking the time to read my question, and even more thanks for answering.