Skip to content

Instantly share code, notes, and snippets.

@isemantics
Created August 29, 2017 04:12
Show Gist options
  • Select an option

  • Save isemantics/5bc92856b65eb35925970acdd003ec5d to your computer and use it in GitHub Desktop.

Select an option

Save isemantics/5bc92856b65eb35925970acdd003ec5d to your computer and use it in GitHub Desktop.
constexpr GCC errors
// Example program
#include <type_traits>
#include <stdint.h>
#include <vector>
#include <cstddef>
#include <iostream>
enum class InputActionType : uint8_t
{
Nothing,
BuildItem,
StartMoving,
};
class InputAction
{
public:
template<InputActionType ActionType, class T>
struct ActionTypeAlias
{
using type = T;
static constexpr InputActionType action_type = ActionType;
static constexpr auto no_data = std::is_void<T>::value;
constexpr operator InputActionType() const
{
return ActionTypeAlias<ActionType, T>::action_type;
}
};
static constexpr ActionTypeAlias<InputActionType::Nothing, void> Nothing = {};
static constexpr ActionTypeAlias<InputActionType::BuildItem, bool> BuildItem = {};
static constexpr ActionTypeAlias<InputActionType::StartMoving, bool> StartMoving = {};
};
class Controller {};
class GameActionHandler
{
public:
using ControllerAction = void (GameActionHandler::*)(const InputAction&, Controller*);
template<class T>
static void addToActions(InputActionType action, T function, std::vector<T>& result)
{
if (result.size() <= size_t(action))
result.resize(size_t(action) + 1);
result[size_t(action)] = function;
}
void buildItem(const InputAction&, Controller*) {}
void startMoving(const InputAction&, Controller*) {}
static std::vector<ControllerAction> getControllerActions()
{
std::vector<ControllerAction> result;
GameActionHandler::addToActions(InputAction::BuildItem, &GameActionHandler::buildItem, result);
GameActionHandler::addToActions(InputAction::StartMoving, &GameActionHandler::startMoving, result);
return result;
}
};
int main()
{
auto actions = GameActionHandler::getControllerActions();
std::cout << actions.size();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment