Check if function argument is a string

Feb 21, 2023 at 10:04am
How do I check if an arg extracted from va_arg inside a function is a string?

My function has variable number of args.
Last edited on Feb 21, 2023 at 10:04am
Feb 21, 2023 at 10:26am
You can't. Use variadic function templates instead.
Feb 21, 2023 at 12:57pm
You appear to be trying to force the language/preprocessor system into doing something it wasn't designed to do. While you might be able to use variadic function templates, this inevitably adds complexity that perhaps you don't need.

If you explained the end goal of what you are trying to do, somebody might be able to offer a better design.
Feb 21, 2023 at 1:27pm
You have to know the type of every specified variadic function argument. That's why with printf() et al (which uses variadic functions) you have to specify exactly the type of each argument in the format string.
Feb 21, 2023 at 4:22pm
Off the top of my head.....

Are you trying to do this check at compile time or runtime? That does make a difference.

With C it ain't possible -- maybe, possibly with a lot of code that makes for overly complex and hard-to-maintain code.

C++ is (probably) able to do what is wanted, using templates and/or concepts. The level of complexity is less, but still a nightmare.

I just spitballing SWAG since I have never tried to rewrite the language to such a degree as you continue to want.
Feb 22, 2023 at 7:02am
You can do that when you use the type/value paradigm.

For instance by prefixing any value with one byte that determines the type. The value 0 might determine the end of the list.
Feb 22, 2023 at 6:52pm

Thanks for the input guys. I'm not really leaning into using variadic macros for this one.


@coder777

Yes, you might be into something there and I did think about this one before.
Topic archived. No new replies allowed.