lua-users home
lua-l archive

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

Here's my current best shot at this:

define these classes:

class Cast {
		virtual int cast() = 0;

	template<class T, class B>
	class Cast_op : public Cast{
		virtual int cast() {
			T t;
			char *p1 = (char *)&t;
			char *p2 = (char *)((B *)&t);
			return p2 - p1;

use them this way:

struct Casting {
		const char *name;
		Udata::Cast *cast;
	Casting cc[] = {
		{"W", new Udata::Cast_op<X, W>},
		{"V", new Udata::Cast_op<X, V>},
		{0, (Udata::Cast *)0}
	printf("Cast<X, W>: %d\n", cc[0].cast->cast());
	printf("Cast<X, V>: %d\n", cc[1].cast->cast());

A bit ridiculous, but it works and can be iterated over, which is what
I'm really going for.