Re: Fwd: variadic macro numerber of params

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



chedi toueiti <chedi.toueiti@xxxxxxxxx> writes:

> I'm trying to write a macro to process a variable argument list but
> need to know exactly the number of argument supplied.
> Is there any way to do so with gcc ??

If all the args are convertible to the same type, you can do
something like this:

#define COUNT_ARGS(...) (sizeof (char []) { __VA_ARGS__ })

Curiously, if I do "return COUNT_ARGS(42, 69, 105, 491);",
gcc-4.4.3 -Wall -Wextra does not warn that the last number
does not fit in char.

With C++, overloading the comma operator seems like a solution:

#include <stdio.h>

template <int C>
struct count_args
{
  char n[C];
  template <typename A> count_args <C + 1> operator , (A) const;
};

#define COUNT_ARGS(...) (sizeof (count_args <0> (), __VA_ARGS__).n)

int main()
{
  return COUNT_ARGS("hobby", -55, printf);
}

I suspect that argument packs and constexpr in C++0x make it
possible to write this in a nicer way.

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux