Skip to content

Instantly share code, notes, and snippets.

@phire
Created April 16, 2013 14:37
Show Gist options
  • Save phire/5396434 to your computer and use it in GitHub Desktop.
Save phire/5396434 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include "typelist.h"
using namespace iatsbg;
// This whole thing would be a lot cleaner if c++ had a static if
#define ADD_RETURN_TYPE(type) \
template<typename T> static T cast(type); \
template<> type cast(type val) { return val; }
ADD_RETURN_TYPE(const char*)
ADD_RETURN_TYPE(int)
// void needs a slightly diffrent template
template<typename T> static T cast();
template<> void cast() {}
#define multiReturn(x) return cast<typename typelist_nth<types, pass>::type>(x)
#define multiFunc template<int pass> typename typelist_nth<types, pass>::type
typedef typelist_make<int, const char *, void> types;
multiFunc test() {
if(pass == 1) {
multiReturn("string");
} else if(pass == 0) {
multiReturn(1);
} else {
printf("This is the 3rd pass!\n");
multiReturn();
}
printf("foobared\n");
}
int main(int argc, char **argv) {
printf("%s\n", test<1>());
printf("%i\n", test<0>());
test<2>();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment