0

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.

10
  • This is a weird thing to do. It forces you to put all handlers in a single translation unit, and at that point, can't you make one big switch? Commented Aug 20, 2023 at 12:03
  • Looks legal to me, MSVC intellisense is buggy. You can try a different IDE, with code completion based on Clangd (VSC can use Clangd), I've never had those issues with it. Commented Aug 20, 2023 at 12:04
  • "Storing func pointers in a container isn't a way because they can't be inlined.": That's not true. If the container is e.g. a constexpr std::array, then I am sure compilers will still be able to inline. Example: godbolt.org/z/a7PdjG5W9 Commented Aug 20, 2023 at 12:05
  • Perhaps the IDE gets confused by your code generation macro that has same name as class template. If a tool gets confused by a piece of code then most of human readers (including its author few weeks later) are confused too.
    – Öö Tiib
    Commented Aug 20, 2023 at 12:09
  • @ÖöTiib also that may break in clang\gcc. Technically that's not quite legal. Commented Aug 20, 2023 at 12:13

0

Browse other questions tagged or ask your own question.