Skip to content

Instantly share code, notes, and snippets.

@stevenRush
Created December 4, 2013 15:28
Show Gist options
  • Save stevenRush/7789496 to your computer and use it in GitHub Desktop.
Save stevenRush/7789496 to your computer and use it in GitHub Desktop.
Some crazy error handling
#pragma once
#include <cstdio>
#include <cstdarg>
template<typename T>
class result
{
private:
T value;
public:
result(T value)
{
this->value = value;
}
bool success()
{
return true;
}
};
template<>
class result<void>
{
private:
char message[256];
public:
result(const char * format, va_list args)
{
vsprintf(message, format, args);
}
bool success()
{
return false;
}
const char * get_message()
{
return message;
}
};
template<typename T>
result result_ok(T value)
{
return result<T>(value);
}
result<void> result_fail(const char * format, ...)
{
va_list args;
va_start(args, format);
result<void> result(format, args);
va_end(args);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment