I want to know that is there any way to call a C VARIADIC MACRO selectively.
First, let me show some code I want to achieve:
#include <stdio.h>
#define _VA_NARGS_IMPL(_1,_2,_3,_4,_5,_6,_7,_8,N,...) N
#define _VA_NARGS(...) _VA_NARGS_IMPL(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1)
#define binder(count, ...) arg##count(__VA_ARGS__)
#define foo(...) binder(_VA_NARGS(__VA_ARGS__), __VA_ARGS__)
#define arg1(_1) _1
#define arg2(_1, _2) _1, _2
#define arg3(_1, _2, _3) _1, _2, _3
int main()
{
printf("%d %d %d", foo(11,22,33));
return 0;
}
I tested it in VC11, GCC4.8 and Clang 3.4 but none of them could compile it as I wanted.
Yes, I want to call a macro by count of its arguments, but macros are expanded to:
foo(...)
binder(count, ...)
arg_VA_NAGS(...)
Isn't there any trick?
EDIT:
I Wrote in more detail about what I really want.
I found some clue from answers and edited my code.
typedef unsigned short ListHeader;
template<typename T>
inline const size_t GetSize(const T& _obj) {return sizeof(T);}
inline const size_t GetSize(const std::string& _str) {return sizeof(ListHeader) + _str.size() + 1;}
inline const size_t GetSize(const std::vector<std::string>& _vec)
{
size_t total = 0;
for (auto item : _vec)
{
total += GetSize(item);
}
return sizeof(ListHeader) + total;
}
template<typename T>
inline const size_t GetSize(const std::vector<T>& _vec)
{
size_t total = 0;
for (auto item : _vec)
{
total += GetSize<decltype(item)>(item);
}
return sizeof(ListHeader) + total;
}
#define VA_NARGS_IMPL(_1,_2,_3,_4,_5,_6,_7,_8,N,...) N
#define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1)
#define VARARG_IMPL2(base, count, ...) base##count(__VA_ARGS__)
#define VARARG_IMPL(base, count, ...) VARARG_IMPL2(base, count, __VA_ARGS__)
#define VARARG(base, ...) VARARG_IMPL(base, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
#define SerialSize(...) VARARG(SerialSize, __VA_ARGS__)
#define SerialSize1(_1) \
const size_t size() {return GetSize(_1);}
#define SerialSize2(_1,_2) \
const size_t size() {return GetSize(_1) + GetSize(_2);}
#define SerialSize3(_1,_2,_3) \
const size_t size() {return GetSize(_1) + GetSize(_2) + GetSize(_3);}
#define SerialSize4(_1,_2,_3,_4) // same implementation except count of arguments: 1..4
#define SerialSize5(_1,_2,_3,_4,_5) // 1...5
#define SerialSize6(_1,_2,_3,_4,_5,_6) //1...6
#define SerialSize7(_1,_2,_3,_4,_5,_6,_7) //1...7
#define SerialSize8(_1,_2,_3,_4,_5,_6,_7,_8) //1..8
// Please don't care about detailed implementation of my Archive class.
// It's not important now I guess..
class Archive
{
public:
template<typename T>
Archive& operator, (T& _val) //comma operator for Variadic macro
{
if (reading)
read(&_val);
else
write(&_val);
return *this;
}
Archive& out();
Archive& in();
private:
template<typename T>
Archive& read(T&);
template<typename T>
Archive& write(T&);
};
class Serializable
{
public:
Serializable(void) {}
virtual ~Serializable(void) {}
virtual const size_t size() = 0;
virtual void serialize(Archive&) = 0;
virtual void deserialize(Archive&) = 0;
};
#define SerialFormat(...) \
SerialSize(__VA_ARGS__) \
void serialize(Archive& ar)\
{\
ar.out() , ##__VA_ARGS__ ;\
}\
void deserialize(Archive& ar)\
{\
ar.in() , ##__VA_ARGS__ ;\
}
//usage:
struct Packet_ReqeustLogin
: public Serializable
{
std::string name;
std::string password;
SerialFormat(name, password);
};
It was tested in Xcode5 and VC11 but It doesn't work in VC11.
VC11's output is like this :
warning C4002: too many actual parameters for macro 'SerialSize1'
What can I do to fix it?