提问人:Deus 提问时间:8/20/2023 更新时间:8/20/2023 访问量:58
实例化期间未定义的 C++ 内联事件
C++ inlined events undefined during instantiation
问:
如果可能的话,我想创建内联事件函数。将 func 指针存储在容器中不是一种方法,因为它们不能内联。事件中的代码将始终被调用,我不想将其添加到一个全局事件函数中。
我的代码:
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();
}
该代码使用 v143 MSVC 进行编译,但是如果我尝试在事件中使用全局变量,我会收到 VS IDE 警告:“标识符”ptr“在 X 行实例化类期间检测到未定义”
它似乎有效,但此代码是否合法并定义了行为? 使用 gcc 使用编译器资源管理器,它也可以毫无问题地编译。
答: 暂无答案
评论
switch
constexpr std::array