#include <iostream>
template <class TIPE, typename T_M> void my_qsort(
TIPE *buf,
int first,
int last,
T_M TIPE::*m
){
std::cout << (buf->*m) << std::endl;
}
struct Color {
union {
char hsv[3];
struct {
char hsv0;
char hsv1;
char hsv2;
};
};
};
int main() {
Color color = { "hs" };
my_qsort(
&color,
0,
0,
&Color::hsv0
);
my_qsort(
&color,
0,
0,
&Color::hsv1
);
return 0;
}
template<typename T>
T cast2T(int dummy, ...){
va_list args;
va_start(args, dummy);
T result = va_arg(args, T);
va_end(args);
return result;
}
template<typename T>
void* cast2ptr(T mp) {
union {
T mp;
void* p;
} caster = {mp};
return caster.p;
}
template<typename T>
T castFromPtr(void* p) {
union {
void* p;
T mp;
} caster = {p};
return caster.mp;
}
int main() {
Color color = { "hs" };
char (Color::*hsvA)[3] = &Color::hsv;
char (Color::*hsvB) = cast2T<char (Color::*)>(0, cast2T<char*>(0, hsvA) + 1);
my_qsort(
&color,
0,
0,
hsvB
);
char (Color::*hsvC) = castFromPtr<char (Color::*)>( ((char*) cast2ptr(hsvA)) + 1);
my_qsort(
&color,
0,
0,
hsvC
);
return 0;
}
#include <iostream>
template <class _Ty, typename _Getter> void my_qsort(_Ty *buf, int first, int last, _Getter g){
for(; first != last; first++) {
std::cout << "VALUE[" << first << "] = " << g(buf[first]) << std::endl;
}
}
struct Color {
double hsv[3];
};
int main() {
Color color[3] = {
{ 0.0, 0.1, 0.0 },
{ 0.0, 0.2, 0.0 },
{ 0.0, 0.3, 0.0 },
};
my_qsort(
color,
0,
3,
[](const Color& c) { return c.hsv[1]; }
);
return 0;
}
// g++ main.cpp -std=c++0x -o main
// ./main
// VALUE[0] = 0.1
// VALUE[1] = 0.2
// VALUE[2] = 0.3
#define FOO_MAGIC protected: foo someMagicFunction(){...} foo Bar;
// далее - мешанина из \ template typedef typename { } <> () FOO_MAGIC_1
// и прочих радостей поддержки чужого кода
int magicFunction(int a, int b, int c, ...) {
va_list args;
va_start(args, c);
//...
}
va_start(arguments, _col);