Skip to content

Instantly share code, notes, and snippets.

@Fireboyd78
Created April 19, 2018 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fireboyd78/9c8903922a52bbbc8433f1f15dd5cfd2 to your computer and use it in GitHub Desktop.
Save Fireboyd78/9c8903922a52bbbc8433f1f15dd5cfd2 to your computer and use it in GitHub Desktop.
C++ Template Brain Fuck
struct badType;
template <typename TType = badType>
struct type_decl {
static constexpr const char * format_string() {
return (std::is_integral_v<TType>
? "%d"
: (std::is_floating_point_v<TType>
? "%f"
: (std::is_pointer_v<TType>
? "%X"
: "")
)
);
}
};
template <>
struct type_decl<const char *> {
static constexpr const char * format_string() {
return "%s";
}
};
template <typename TType>
constexpr const char * get_fmt_string(TType value) {
return type_decl<TType>::format_string();
};
template <typename ...TArgs>
void brain_fuck(TArgs ...args) {
using unpacker_t = int[];
string_buf<4096> buffer;
int num_args = 0;
(void)unpacker_t
{
(
args,
buffer.append(" arg[%d] : ", num_args++),
buffer.append("%s : ", typeid(args).name()),
buffer.append("%X : '", sizeof(args)),
buffer.append(get_fmt_string(args), args),
buffer.append("'\n"),
0)...,
};
debugf("**** %d args ****\n", num_args);
debugf(buffer);
}
brain_fuck("oh", "my", "god", 2.0f, 1, true);
/*
Output:
**** 6 args ****
arg[0] : char const * : 4 : 'oh'
arg[1] : char const * : 4 : 'my'
arg[2] : char const * : 4 : 'god'
arg[3] : float : 4 : '2.000000'
arg[4] : int : 4 : '1'
arg[5] : bool : 1 : '1'
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment