How to implement variable amount of parameters?
-
OK. hope my English is good enough to ask this and GET a real answer. I been looking at "using ellipsis " to implement variable count of parmaters passed to a function , for years... Never did figure out the tech gobly (sic?) gook / tech talk explaining how to implement it. I have a sort-off application to pass variable amount of SAME type of parameters and I am still curious and like to finally learn how to do it. I am not asking how to pass an array or pointer. I need this
QString BT_Utility_Library::
ProcessCommand ( char* command, QString verify...QString verify_1 ) -
OK. hope my English is good enough to ask this and GET a real answer. I been looking at "using ellipsis " to implement variable count of parmaters passed to a function , for years... Never did figure out the tech gobly (sic?) gook / tech talk explaining how to implement it. I have a sort-off application to pass variable amount of SAME type of parameters and I am still curious and like to finally learn how to do it. I am not asking how to pass an array or pointer. I need this
QString BT_Utility_Library::
ProcessCommand ( char* command, QString verify...QString verify_1 )Easy peasy :)
#include
void ProcessCommand (char* command, ...)
{
va_list params; //opaque structure needed to process variable arguments listsva_start (params, command); //initialize with the last fixed argument before ellipsis
// You need to know how many arguments to expect. Assume that args_count function tells you that
// args_count is your function not some standard function
int narg=args_count(command);
for (i=0; i
One of the problems with this code is that you need to know how many arguments to expect and the type of each one. Functions likeprintf
explore format string and figure out from the '%' specifiers. There are countless stories about bugs occurring because they don't match. Sometimes, specially if your arguments are of the same type, you can add a count parameter just before the variable part:void ProcessCommand (char* command, int count, ...)
Another thought: if you know that all parameters are of the same type, why not pass an array. Something like this:
void ProcessCommand (char *command, std::vector verify)
{
for (auto& pstr : verify)
{
//do something with it
}
}Mircea
-
OK. hope my English is good enough to ask this and GET a real answer. I been looking at "using ellipsis " to implement variable count of parmaters passed to a function , for years... Never did figure out the tech gobly (sic?) gook / tech talk explaining how to implement it. I have a sort-off application to pass variable amount of SAME type of parameters and I am still curious and like to finally learn how to do it. I am not asking how to pass an array or pointer. I need this
QString BT_Utility_Library::
ProcessCommand ( char* command, QString verify...QString verify_1 )This article can probably explain it a lot better than I can. In particular I did not know that the argument before the ellipses must not be something that is eligible for default parameter promotion (e.g a float, which gets promoted to double). [Modern C++ and Variadic Functions: How to Shoot Yourself in the Foot and How to Avoid It](https://www.linkedin.com/pulse/modern-c-variadic-functions-how-shoot-yourself-foot-avoid-zinin) You should take note of the discussion of how the variadic function determines how many arguments it has. I also have an idea that stdargs may not be the preferred way to write a variadic function in C++ any longer. There's a tickle in the back of my brain that perhaps variadic templates are a better way to go. If you're interested in that, check out Jason Turners C++ weekly episodes on You Tube: [C++ Weekly - Ep 6 Intro To Variadic Templates - YouTube](https://www.youtube.com/watch?v=o1EvPhz6UNE&list=PLs3KjaCtOwSZ2tbuV1hx8Xz-rFZTan2J1&index=6)
Keep Calm and Carry On
-
OK. hope my English is good enough to ask this and GET a real answer. I been looking at "using ellipsis " to implement variable count of parmaters passed to a function , for years... Never did figure out the tech gobly (sic?) gook / tech talk explaining how to implement it. I have a sort-off application to pass variable amount of SAME type of parameters and I am still curious and like to finally learn how to do it. I am not asking how to pass an array or pointer. I need this
QString BT_Utility_Library::
ProcessCommand ( char* command, QString verify...QString verify_1 )It dpends om what type the extra parameters are. If they are all the same (e.g. all ints, chars etc.) then you are better using an array or a C++ container. But if you are passing a variable number of varying types then you need some way of identifying which type each parameter is. In the C library printf/scanf functions this is done with the format string, where each control item in the string tells the function what type to expect for the corresponding variable.
-
OK. hope my English is good enough to ask this and GET a real answer. I been looking at "using ellipsis " to implement variable count of parmaters passed to a function , for years... Never did figure out the tech gobly (sic?) gook / tech talk explaining how to implement it. I have a sort-off application to pass variable amount of SAME type of parameters and I am still curious and like to finally learn how to do it. I am not asking how to pass an array or pointer. I need this
QString BT_Utility_Library::
ProcessCommand ( char* command, QString verify...QString verify_1 )Besides the other answers you might want to ask how many of these would be reasonable in the next 5 years of using this method. So if you start with 2 now and there are 4 in 5 years ok. But if you start with 2 now and in three months there are 20 then that is a problem. In that case a collection of some sort is better.
-
Besides the other answers you might want to ask how many of these would be reasonable in the next 5 years of using this method. So if you start with 2 now and there are 4 in 5 years ok. But if you start with 2 now and in three months there are 20 then that is a problem. In that case a collection of some sort is better.
" in three months there are 20" Nah, they're on to the next project.... who cares about code maintenance? :)
Charlie Gilley “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759 Has never been more appropriate.