I want to create inlined event functions if possible. Storing func pointers in a container isn't a way because they can't be inlined. The code in the event will be called always and I don't want to add it to one global event function.
My code:
enum class eGameEvent
{
LOOP,
LOAD
};
template<size_t n>
class GameEvent
{
public:
const static eGameEvent s_event;
inline static void Event() { }
};
#define GameEvent(t_event) \
template<> const eGameEvent GameEvent<__COUNTER__>::s_event = t_event; \
template<> void GameEvent<__COUNTER__>::Event() \
class c{};
std::unique_ptr<c> ptr;
GameEvent(eGameEvent::LOOP)
{
//VS: identifier "ptr" is undefined detected during instantiation of class at line
//but compiles and works as i want
if(ptr)
{
std::cout <<"test\n";
}
}
template <std::size_t... Is>
constexpr auto OddIndicesHelper(std::index_sequence<Is...>)
{
return std::index_sequence<(2 * Is + 1)...>{};
}
template <std::size_t N>
using OddIndices = decltype(OddIndicesHelper(std::make_index_sequence<N / 2>{}));
template<eGameEvent Event>
constexpr void DeclareEvents()
{
[] <std::size_t... I>
(std::index_sequence<I...>)
{
(((GameEvent<I - 1>::s_event == Event) ? GameEvent<I>::Event() : [] {}()), ...);
}(OddIndices<__COUNTER__>{});
}
void Loop()
{
DeclareEvents<eGameEvent::LOOP>();
};
int main()
{
ptr = std::make_unique<c>();
while(1) Loop();
}
The code compiles with v143 MSVC, but if I try to use global variable in an event, I am getting VS IDE warning: "identifier "ptr" is undefined detected during instantiation of class at line X"
It seems to works, but is this code even legal and has defined behavior? With gcc using the compiler explorer it also compiles without problems.
switch
?constexpr std::array
, then I am sure compilers will still be able to inline. Example: godbolt.org/z/a7PdjG5W9