I was trying to remake some generic containers from c++’s STL but in C.
For instance, this std::pair implementation works in C23:
#define Pair(T1, T2)
struct Pair_##T1##_##T2 {
T1 first;
T2 second;
}
But in older versions i get struct redefinition error if Pair is used more than once.
I came up with a simple solution:
#define Pair_define(T1, T2)
struct Pair_##T1##_##T2 {
T1 first;
T2 second;
}
#define Pair(T1, T2) struct Pair_##T1##_##T2
Where Pair_define is designed to be used once as a definition.
However it produces the same error when used more than once.
I would like to keep the C++ – like syntax but make it more foolproof
//C++
std::pair<int, char> p1 = std::make_pair(4, 'c');
//C
Pair(int, char) p2 = Pair_make(4, 'c');
Tried adding Pair_define macro, but i can’t protect it against multiple use.
Bartek_0x00 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.